The ParamArrayAttribute indicates that a method parameter is a parameter array. A parameter array allows the specification of an unknown number of arguments. A parameter array must be the last parameter in a formal parameter list, and it must be a single-dimension array. When the method is called, a parameter array permits arguments to a method to be specified in either of two ways:
As a single expression of a type that is implicitly convertible to the parameter array type. The parameter array functions as a value parameter.
As zero or more arguments where each argument is an expression of a type that is implictly convertible to the type of the parameter array element.
The example illustrates both calling conventions.
Note: |
|---|
Typically, the ParamArrayAttribute is not used directly in code. Instead, individual language keywords, such as ParamArray in Visual Basic and params in C#, are used as wrappers for the ParamArrayAttribute class. Some languages, such as C#, may even require the use of the language keyword and prohibit the use of ParamArrayAttribute. |
In performing overload resolution, compilers that support parameter arrays substitute a call to the method that includes a parameter array if the method that is actually called in code does not exist but has one fewer parameter than the method that includes the parameter array. For example, a call to the String.Split() instance method with no parameters (a method that does not actually exist in the String class) is resolved as a call to the String..::.Split(array<Char>[]()[]) method. The compiler also passes an empty array of the required type to the method. This means that, when the method processes the elements in the parameter array, it must always be prepared to handle an array whose length is zero. The example provides an illustration.
For more information about using attributes, see Extending Metadata Using Attributes.