This is a brief explanation on how params
and __arglist
work:
When you write
void MyFunction(int a, double b, params string[] c)
{
some code
}
You actually just do the following:
void MyFunction(int a, double b, [ParamArrayAttribute] string[] c)
{
some code
}
The ParamArrayAttribute just instructs the compiler to allow calls of the form:
MyFunction(3, 1.2, "foo", "bar", "baz");
That happens in addition to the "correct" way to call the method
MyFunction(3, 1.2, new string[] {"foo", "bar", "baz"});
The compiler simply convert the first call to the second one, it is just syntactic sugar and nothing more. The method MyFunction has 3 parameters:
- int a
- double b
- string[] c
On the other hand, printf
does not take 2 arguments, one of type string and one of type object[], printf
actually does not even know what a .net string
, object
or object[]
even is. printf
is defined as: (in C)
int __cdecl printf(const char *Format, ...);
printf
takes one argument of Type char *
(the .net interop automatically converts the .net string
object to the unmanaged char *
type), and then it can optionally take any number of additional arguments of any (unmanaged) type. Here is the crucial part, this happens by pushing the additional parameters in the stack and not by pushing an array pointer with the additional parameters, which is what the inperop thinks due to your deceleration. c# does not support this functionality... well... exept if you consider the "unsupported __arglist
keyword", as per Microsoft sayings (check out Compiler Error CS1952 to laugh at their correction suggestion). So, c# actually supports varargs afterall.
Now, the 1363904384 is just the pointer to an array containing the value 10, the .net inperop thinks it should pass an array as an argument (in the stack) while printf wants to have the value as an argument (in the stack).
PS: The Interop marshals the object[]
as an int[]
for your particular case, if you try to pass an object (besides string
) you will get an exception. Object arrays do not make much sense outside the scope of .net, objects in general do not make sense (thought, arrays do make sense).