【发布时间】:2021-03-10 16:09:20
【问题描述】:
当我创建没有构造函数和构造函数的类时,IL 代码不会改变。我认为总是存在构造函数是错误的吗?
namespace Test13
{
class Human
{
}
}
在下面的 IL 代码中,行 -
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
在不声明构造函数的情况下调用构造函数。
.class private auto ansi beforefieldinit
Test13.Human
extends [System.Runtime]System.Object
{
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
.maxstack 8
// [11 9 - 11 23]
IL_0000: ldarg.0 // this
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
IL_0006: nop
// [12 9 - 12 10]
IL_0007: nop
// [14 9 - 14 10]
IL_0008: ret
} // end of method Human::.ctor
} // end of class DecompilationLearn.Human
如果有人能帮我解决这个问题,谢谢。
【问题讨论】:
-
是的,如果我们不定义一个默认无参数
-
请注意,静态类没有有任何构造函数。
-
如果没有构造函数会怎样?您将如何使用(创建一个实例)该类?
标签: c# class oop constructor