I love my day job. It lets me explore all sorts of weird nooks and crannies inside the CLR and it gives me an opportunity to share what I found with you. That’s fun.
Today’s topic is parameter arrays. They are described in Section 10.5.1.4 of the C# Language Specification. Today, I needed a quick and dirty way to cruft up an object that would hold an arbitrary number of values, each of which could be a different type. I turned to parameter arrays for the solution. As it turns out, parameter arrays are simply some syntactic sugar that the C# compiler uses to hide a bit of ugliness from you.
Consider this code frag:
class Values
{
private object[] _parameters;
public Values( params object[] parameters )
{
_parameters = new object[ parameters.Length ];
parameters.CopyTo( _parameters, 0 );
}
}
Notice the constructor accepts a parameter array of object parameters. Now let’s consider a method that passes a bunch of values to the Values constructor:
public static int Add( int x, int y, string[] args )
{
Values values = new Values( x, y, args );
return x + y;
}
If we use ILDASM to examine the CIL that is generated by the C# compiler, this is what we see:
`.method public hidebysig static int32 Add(int32 x, int32 y, string[] args) cil managed { // Code size 40 (0x28) .maxstack 3 .locals init ([0] object[] values)
// Values values = new Values( x, y, args );IL_0000: ldc.i4.3
IL_0001: newarr [mscorlib]System.Object
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.0
IL_0009: ldarg.0
IL_000a: box [mscorlib]System.Int32
IL_000f: stelem.ref
IL_0010: ldloc.0
IL_0011: ldc.i4.1
IL_0012: ldarg.1
IL_0013: box [mscorlib]System.Int32
IL_0018: stelem.ref
IL_0019: ldloc.0
IL_001a: ldc.i4.2
IL_001b: ldarg.2
IL_001c: stelem.ref
IL_001d: ldloc.0
IL_001e: newobj instance void ParameterArrays.Values::.ctor(object[])
IL_0023: pop
// return x + y;
IL_0024: ldarg.0 IL_0025: ldarg.1 IL_0026: add IL_0027: ret } // end of method Class1::ExplicitAdd`
Note that this code was compiled using the /optimize switch so we see the real CIL, and not the wierd crap that is generated to make debuggers happy.
As you can see, the C# compiler generated quite a bit of code for the call to the Values constructor. All this code does is create an object array with three elements, stuff the values of x, y and args into those elements, and pass a reference to that object array to the Values constructor. Note that since we are passing an object array, it is necessary to box the integer values x and y prior to stuffing them in the parameter array.
In fact, the original Add() method is identical to the following ExplicitAdd() method that manually constructs an object array:
public static int ExplicitAdd( int x, int y, string[] args )
{
object[] values = new object[ 3 ];
values[ 0 ] = x;
values[ 1 ] = y;
values[ 2 ] = args;
Values data = new Values( values );
return x + y;
}
Run this code through your C# compiler; you will see that it generates exactly the same CIL. There are other places in C# where the compiler generates a bunch of code to hide ugliness from you. The way delegates are handled is an example of this. I’ll cover that in a future entry.