为什么将 C# 代码编译为 IL 时会创建 .ctor()?
通过这个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它是默认的无参数构造函数。你是对的;它不执行任何操作(除了传递给基本
Object()
构造函数,该构造函数本身也不执行任何特殊操作)。如果没有定义任何其他构造函数,编译器始终为非静态类创建默认构造函数。然后将所有成员变量初始化为默认值。这样您就可以避免
遇到错误。
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
without running into errors.
C# 语言规范的第 10.11.4 节对此进行了介绍
这里
Hello
没有定义的构造函数,因此编译器插入默认的不执行任何操作的构造函数,该构造函数仅调用基类/对象版本This is covered in section 10.11.4 of the C# language spec
Here
Hello
has no defined constructor hence the compiler inserts the default do nothing constructor which just calls the base / object version未定义构造函数的类将获得隐式公共默认构造函数。
仅当基类具有可访问的无参数构造函数时,这才有效。
A class for which you don't define a constructor gets an implicit public default constructor.
This only works if the base class has an accessible parameterless constructor.
class Hello 继承了 object,默认生成的构造函数只是调用类
object
的构造函数。class Hello inherits object, the default generated constructor simply calls the constructor of class
object
.我想规范规定,由于您的类本身不是静态或抽象的,因此它必须公开默认的无参数构造函数。这样,您构建的任何库或 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.