does valueType.ToString() does a cast on the valueType?

前端 未结 2 1171
我寻月下人不归
我寻月下人不归 2020-12-20 23:26

lets say, i have the following code in c#

int x = 0;
x.ToString();

does this internally does a boxing of x? Is there a way to see this hap

2条回答
  •  眼角桃花
    2020-12-20 23:45

    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 taking place.

    On the other hand, this code

    object x = 0;
    x.ToString();
    

    will not surprisingly cause boxing:

    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
    

    Generally, if if the type of x is not int but any value type (struct) then you have to override ToString to avoid boxing. Specifically, a constrained callvirt is emited:

    • If thisType is a value type and thisType implements method then ptr is passed unmodified as the 'this' pointer to a call method instruction, for the implementation of method by thisType.

    • 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.

    If you want to avoid boxing when calling Equals, GetHashCode and ToString on a value type you need to override these methods.

提交回复
热议问题