为什么将 C# 代码编译为 IL 时会创建 .ctor()?

发布于 2024-12-01 21:16:44 字数 1071 浏览 2 评论 0原文

通过这个简单的 C# 代码,我运行 csc hello.cs; ildasm /out=hello.txt hello.exe

class Hello
{
    public static void Main()
    {
        System.Console.WriteLine("hi");
    }
}

这是来自 ildasm 的 IL 代码。

.class private auto ansi beforefieldinit Hello
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    .entrypoint
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "hi"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Hello::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Hello::.ctor

} // end of class Hello

.method public instance void .ctor() 代码有什么用?它似乎没有做任何事情。

With this simple C# code, I run csc hello.cs; ildasm /out=hello.txt hello.exe.

class Hello
{
    public static void Main()
    {
        System.Console.WriteLine("hi");
    }
}

This is the IL code from ildasm.

.class private auto ansi beforefieldinit Hello
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    .entrypoint
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "hi"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Hello::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Hello::.ctor

} // end of class Hello

What's the use of .method public instance void .ctor() code? It doesn't seem to do anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒解孤独 2024-12-08 21:16:44

它是默认的无参数构造函数。你是对的;它不执行任何操作(除了传递给基本 Object() 构造函数,该构造函数本身也不执行任何特殊操作)。

如果没有定义任何其他构造函数,编译器始终为非静态类创建默认构造函数。然后将所有成员变量初始化为默认值。这样您就可以避免

new Hello();

遇到错误。

It's the default parameterless constructor. You're correct; it doesn't do anything (besides passing on to the base Object() constructor, which itself doesn't do anything special either anyway).

The compiler always creates a default constructor for a non-static class if there isn't any other constructor defined. Any member variables are then initialized to defaults. This is so you can do

new Hello();

without running into errors.

眼趣 2024-12-08 21:16:44

C# 语言规范的第 10.11.4 节对此进行了介绍

如果类不包含实例构造函数声明,则会自动提供默认实例构造函数。该默认构造函数只是调用直接基类的无参数构造函数

这里 Hello 没有定义的构造函数,因此编译器插入默认的不执行任何操作的构造函数,该构造函数仅调用基类/对象版本

This is covered in section 10.11.4 of the C# language spec

If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class

Here Hello has no defined constructor hence the compiler inserts the default do nothing constructor which just calls the base / object version

你的心境我的脸 2024-12-08 21:16:44

未定义构造函数的类将获得隐式公共默认构造函数。

public MyClass()
  :base()
{
}

仅当基类具有可访问的无参数构造函数时,这才有效。

A class for which you don't define a constructor gets an implicit public default constructor.

public MyClass()
  :base()
{
}

This only works if the base class has an accessible parameterless constructor.

墨小墨 2024-12-08 21:16:44

class Hello 继承了 object,默认生成的构造函数只是调用类 object 的构造函数。

class Hello inherits object, the default generated constructor simply calls the constructor of class object.

若沐 2024-12-08 21:16:44

我想规范规定,由于您的类本身不是静态或抽象的,因此它必须公开默认的无参数构造函数。这样,您构建的任何库或 PE 的其他用户都可以实例化您的类的副本。

我想,如果它没有 .ctor,它可以被解释为有一个私有 .ctor。一般来说,它是相当模糊的。但从逻辑上讲,你是对的,在这种情况下不需要 .ctor 。

I would imagine the specification stipulates that since your class itself is not static or abstract, it must expose a default parameterless constructor. This way, other users of whatever library or PE you build can instantiate a copy of your class.

If it didn't have a .ctor, it could be construed as having a private .ctor, I suppose. It's generally pretty vague. But logically, you're right, there is no need for the .ctor in this instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文