Variable Number of Members
. NET Framework developer Guide
Variable Number of Members
An array is used to pass a variable number of parameters to members. Some languages (such as C #) provide a keyword to modify the array for passing variable parameters. Paramarrayattribute provides this function for languages that do not provide keywords. This keyword and attribute affect the last parameter of the member signature. This parameter must be a one-dimensional array.
The following code example demonstrates how to define and call a method with a variable number of parameters. Note that in the demonstratevariableparameters method, parameters before usevariableparameters are not put into an array.
C #
Copy code
Public static void usevariableparameters (Params int [] list)
{
For (INT I = 0; I <list. length; I ++)
{
Console. writeline (list [I]);
}
Console. writeline ();
}
Public static void demonstratevariableparameters ()
{
Manager. usevariableparameters (1, 2, 3, 4, 5 );
}
The following guidelines help you understand when variable arrays are used as parameters.
If the end user needs to pass a few elements, consider adding the Params keyword to the array parameter.
Generally, if developers want to pass many elements, the Params keyword may be of little use, because developers are unlikely to pass a large number of objects inline.
If the caller almost always puts the input in an array, do not use the Params array.
For example, byte data is usually stored and processed in byte arrays. Generally, adding the Params keyword to the byte array parameter does not solve the problem, because developers usually do not use a single byte that is not stored in the byte array.
If the array is modified by a member using the Params array parameter, do not use the Params array.
The Common Language Runtime Library (CLR) may have created a temporary array object. If the method modifies the temporary array, these modifications are not available to the caller.
Consider using the Params keyword in a simple overload, even if the more complex overload cannot use it.
Using the Params array in an overload (even if not all the reloads) is beneficial to developers.
Sort the parameters as much as possible so that they can use the Params keyword.
This means that, as long as possible, the array parameter should be the last parameter specified. The following code example shows that the parameter sorting is incorrect.
C #
Copy code
Public int add (int I, Int J, short numberbase)
C #
Copy code
Public int add (int I, Int J, int K, short numberbase)
C #
Copy code
// Can't use Params array.
Public int add (INT [] numbers, short numberbase)
These parameters should be re-sorted as follows:
C #
Copy code
Public int add (short numberbase, int I, Int J)
C #
Copy code
Public int add (short numberbase, int I, Int J, int K)
C #
Copy code
// Can use Params array.
Public int add (short numberbase, Params int [] numbers)
Special overloading and code paths are provided for calls using a small number of parameters in extremely performance-sensitive APIs.
According to this rule, you can avoid creating an array when calling a member with a small number of parameters. The parameter name should be in the singular form of the array parameter, followed by a numeric suffix. The following code example demonstrates a member signature that complies with this criterion.
C #
Copy code
Public static void writeline (
String format,
Object arg0,
Object arg1,
Object arg2
)
Note that null (nothing in Visual Basic) can be passed as a Params array parameter.
The member should check the null array before processing it.
Do not use the varargs method. This method is expressed as a ellipsis.
Because varargs call conventions do not comply with CLs, they should not be used in public members. It can be used internally.
Part of the copyright is 2005 Microsoft Corporation. All rights reserved.
Some copyrights are reserved for Addison-Wesley Corporation. All rights reserved.
For more information about the Design Guide, see Framework Design Guidelines: Conventions, idioms, and patterns for reusable, edited by Krzysztof cwalina and Brad Abrams and published by Addison-Wesley in 2005. net libraries (Framework Design Guide: reusable. net Library conventions, terms, and patterns).
See
Concept
Parameter Design
Other resources
Member Design Guidelines
Design Guidelines for Class Library Development # C # column