I was just participating in Stack Overflow question Is everything in .NET an object?.
And one poster (in comments of accepted answer) seemed to think that performing a method call on a value type resulted in boxing. He pointed me to Boxing and Unboxing (C# Programming Guide) which doesn't exactly specify the use case we're describing.
I'm not one to trust a single source, so I just wanted to get further feedback on the question. My intuition is that there is no boxing but my intuition does suck. :D
To further elaborate:
The example I used was:
int x = 5;
string s = x.ToString(); // Boxing??
Boxing does not occur if the struct in question overrides the method inherited from the object as the accepted answer here states.
However if the struct doesn't override the method, a "constrain" CIL command is executed prior to a callvirt. According to the documentation, OpCodes.Constrained Field, this results in boxing:
If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the 'this' pointer to the callvirt method instruction.
In the case you have given the answer is no, as plinth pointed out.
However, it will if you call a method through an interface pointer.
Consider the code:
Then
Does not result in boxing:
However, this does:
Here's the IL for your code:
So the answer in this case is no.
@ggf31316
I have checked ToString for you. Int32 does override ToString, so I made a struct that doesn't. I used .NET Reflector to ensure that the struct wasn't somehow magically overriding ToString(), and it wasn't.
So the code was like this:
And the MSIL (via ILDASM) for the Main method is this:
Now, despite no boxing call taking place, if you check the documentation about a constrained + a call virt, you will find it states that boxing DOES take place. oOo
Quote:
I believe that calling ToString, Equals and Gethashcode result in boxing if the structure does not override the methods.