Here is the IL generated by your code:
IL_0001: ldc.i4.0
IL_0002: stloc.0 // x
IL_0003: ldloca.s 00 // x
IL_0005: call System.Int32.ToString
As you can see, no boxing is happening.
On the other hand, this code
object x = 0; x.ToString();
boxing will not be surprising:
IL_0001: ldc.i4.0
IL_0002: box System.Int32
IL_0007: stloc.0 // x
IL_0008: ldloc.0 // x
IL_0009: callvirt System.Object.ToString
In general, if type x
not int
, but any type of value ( struct
), you must override ToString
to avoid boxing. In particular, constrained callvirt
stands out:
If thisType is a value type, and thisType implements the method, then ptr is passed unchanged as the 'this' pointer to the invocation method instruction to implement the method with this type.
If thisType is a value type, and this type does not implement the method, then ptr is dereferenced, placed in a box, and passed as the 'this' pointer to the callvirt method command.
If you want to avoid boxing when calling Equals
, GetHashCode
and ToString
by value type, you need to override these methods.
source share