25

我刚刚参加 Stack Overflow 问题.NET 中的所有内容都是对象吗?.

一位发帖人(在接受答案的评论中)似乎认为对值类型执行方法调用会导致装箱。他向我指出了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 方法指令。

4

4 回答 4

18

这是您的代码的 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

因此,在这种情况下,答案是否定的。

于 2009-01-12T18:14:58.427 回答
12

正如 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 
于 2009-01-12T18:30:41.483 回答
7

@ ggf31316

“我相信如果结构不覆盖方法,调用 ToString、Equals 和 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

现在,尽管没有发生拳击呼叫,但如果您查看有关 constrained + a call virt 的文档,您会发现它声明确实发生了拳击。哦哦

引用:

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

于 2009-01-13T11:30:05.700 回答
4

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

于 2009-01-13T00:26:58.720 回答