【问题标题】:Does calling a method on a value type result in boxing in .NET?对值类型调用方法会导致 .NET 中的装箱吗?
【发布时间】:2010-09-30 23:37:39
【问题描述】:

我刚刚参加了 Stack Overflow 问题Is everything in .NET an object?

并且一位发帖者(在接受答案的 cmets 中)似乎认为对值类型执行方法调用会导致装箱。他将我指向 Boxing and Unboxing (C# Programming Guide),这并没有准确说明我们所描述的用例。

我不是一个相信单一来源的人,所以我只是想获得关于这个问题的进一步反馈。我的直觉是没有拳击,但我的直觉确实很糟糕。 :D

进一步阐述:

我用的例子是:

int x = 5;
string s = x.ToString(); // Boxing??

如果有问题的结构覆盖从对象继承的方法,则不会发生装箱,正如此处接受的答案所述。

但是,如果结构没有覆盖该方法,则会在 callvirt 之前执行“约束”CIL 命令。根据文档,OpCodes.Constrained Field这会导致拳击

如果 thisType 是一个值类型并且 thisType 没有实现方法 然后 ptr 被取消引用,装箱,并且 作为“this”指针传递给 callvirt 方法说明。

【问题讨论】:

标签: .net boxing value-type


【解决方案1】:

这是您的代码的 IL:

L_0001: ldc.i4.5      // get a 5 on the stack
L_0002: stloc.0       // store into x
L_0003: ldloca.s x    // get the address of x on the stack
L_0005: call instance string [mscorlib]System.Int32::ToString()  // ToString
L_000a: stloc.1       // store in s

所以在这种情况下的答案是否定的。

【讨论】:

  • 这里重要的一点是,ToString 方法不是针对 x 的值调用的,而是针对 x 的地址调用的。注意 ldloca 指令。
【解决方案2】:

正如 plinth 指出的那样,如果您给出的答案是否定的。

但是,如果您通过接口指针调用方法,它会。

考虑代码:

interface IZot
{
    int F();
}

struct Zot : IZot
{
    public int F()
    {
        return 123;
    }
}

然后

Zot z = new Zot();
z.F();

不会导致拳击:

.locals init (
    [0] valuetype ConsoleApplication1.Zot z)
L_0000: nop 
L_0001: ldloca.s z
L_0003: initobj ConsoleApplication1.Zot
L_0009: ldloca.s z
L_000b: call instance int32 ConsoleApplication1.Zot::F()
L_0010: pop 
L_0011: ret 

但是,这样做:

IZot z = new Zot();
z.F();

   .locals init (
        [0] class ConsoleApplication1.IZot z,
        [1] valuetype ConsoleApplication1.Zot CS$0$0000)
    L_0000: nop 
    L_0001: ldloca.s CS$0$0000
    L_0003: initobj ConsoleApplication1.Zot
    L_0009: ldloc.1 
    L_000a: box ConsoleApplication1.Zot
    L_000f: stloc.0 
    L_0010: ldloc.0 
    L_0011: callvirt instance int32 ConsoleApplication1.IZot::F()
    L_0016: pop 

【讨论】:

    【解决方案3】:

    我相信如果结构不覆盖方法,调用 ToString、Equals 和 Gethashcode 会导致装箱。

    【讨论】:

      【解决方案4】:

      @ggf31316

      “我相信调用 ToString, 等于和 Gethashcode 导致 如果结构不装箱 覆盖方法。”

      我已经为您检查了 ToString。 Int32 确实覆盖了 ToString,所以我创建了一个没有覆盖的结构。我使用.NET Reflector 来确保该结构不会以某种方式神奇地覆盖 ToString(),但事实并非如此。

      所以代码是这样的:

      using System;
      
      namespace ConsoleApplication29
      {
          class Program
          {
              static void Main(string[] args)
              {
                  MyStruct ms = new MyStruct(5);
                  string s = ms.ToString();
                  Console.WriteLine(s);
              }
          }
      
          struct MyStruct
          {
              private int m_SomeInt;
      
              public MyStruct(int someInt)
              {
                  m_SomeInt = someInt;
              }
      
              public int SomeInt
              {
                  get
                  {
                      return m_SomeInt;
                  }
              }
          }
      }
      

      Main 方法的 MSIL(通过 ILDASM)是这样的:

        IL_0000:  ldloca.s   ms
        IL_0002:  ldc.i4.5
        IL_0003:  call       instance void ConsoleApplication29.MyStruct::.ctor(int32)
        IL_0008:  ldloca.s   ms
        IL_000a:  constrained. ConsoleApplication29.MyStruct
        IL_0010:  callvirt   instance string [mscorlib]System.Object::ToString()
        IL_0015:  stloc.1
        IL_0016:  ldloc.1
        IL_0017:  call       void [mscorlib]System.Console::WriteLine(string)
        IL_001c:  ret
      

      现在,尽管没有进行拳击通话,但如果您检查the documentation about a constrained + a call virt,您会发现它表明拳击确实发生了。哦哦

      引用:

      如果 thisType 是一个值类型并且 thisType 没有实现方法 然后 ptr 被取消引用,装箱,并且 作为“this”指针传递给 callvirt 方法说明。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 2011-04-03
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        相关资源
        最近更新 更多