vararg methods accept a variable number of arguments. They shall use the vararg calling convention (§[II.15.3(#todo-missing-hyperlink)).
At each call site, a method reference shall be used to describe the types of the fixed and variable arguments that are passed. The fixed part of the argument list shall be separated from the additional arguments with an ellipsis (see Partition I).
[Note: The method reference is represented by either a MethodRef (§II.22.25) or MethodDef (§II.22.26). A MethodRef might be needed even if the method is defined in the same assembly, because the MethodDef only describes the fixed part of the argument list. If the call site does not pass any additional arguments, then it can use the MethodDef for vararg methods defined in the same assembly. end note]
The vararg arguments shall be accessed by obtaining a handle to the argument list using the CIL instruction arglist
(see Partition III). The handle can be used to create an instance of the value type System.ArgIterator
which provides a type-safe mechanism for accessing the arguments (see Partition IV).
[Example: The following example shows how a vararg method is declared and how the first vararg argument is accessed, assuming that at least one additional argument was passed to the method:
.method public static vararg void MyMethod(int32 required) {
.maxstack 3
.locals init (valuetype [mscorlib]System.ArgIterator it, int32 x)
ldloca it // initialize the iterator
initobj valuetype [mscorlib]System.ArgIterator
ldloca it
arglist // obtain the argument handle
call instance void [mscorlib]System.ArgIterator::.ctor(valuetype
[mscorlib]System.RuntimeArgumentHandle) // call constructor of iterator
/* argument value will be stored in x when retrieved, so load address of x */
ldloca x
ldloca it
// retrieve the argument, the argument for required does not matter
call instance typedref [mscorlib]System.ArgIterator::GetNextArg()
call object [mscorlib]System.TypedReference::ToObject(typedref) /* retrieve the object */
castclass [mscorlib]System.Int32 // cast and unbox
unbox int32
cpobj int32 // copy the value into x
// first vararg argument is stored in x
ret
}
end example]