C#/.NET has variadic function parameters by passing an Array
type by-reference (as opposed to C/C++ which just places all of the values directly on the stack, for better and for worse).
In the C# world this has a neat advantage of allowing you to call the same function with either 'raw' arguments or a reusable array instance:
CultureInfo c = CultureInfo.InvariantCulture;
String formatted0 = String.Format( c, "{0} {1} {2}", 1, 2, 3 );
Int32 third = 3;
String formatted0 = String.Format( c, "{0} {1} {2}", 1, 2, third );
Object[] values = new Object[] { 1, 2, 3 };
String formatted1 = String.Format( c, "{0} {1} {2}", values );
This means that the generated CIL is equivalent to:
String formatted0 = String.Format( c, "{0} {1} {2}", new Object[] { 1, 2, 3 } );
Int32 third = 3;
String formatted0 = String.Format( c, "{0} {1} {2}", new Object[] { 1, 2, third } );
Object[] values = new Object[] { 1, 2, 3 };
String formatted1 = String.Format( c, "{0} {1} {2}", values );
Which means that (in a non-optimizing JIT compiler) every call will allocate a new Object[]
instance - though in the third example you're able to store the array as a field or other reusable value to eliminate the new allocation on every call to String.Format
.
But in the official CLR runtime and JIT are any optimizations done to eliminate this allocation? Or perhaps is the array tagged specially so that it will be deallocated as soon as execution leaves the scope of the call-site?
Or, perhaps, because the C# or JIT compiler knows the number of arguments (when used "raw") could it do the same thing as the stackalloc
keyword and place the array on the stack, and thus not need to deallocate it?