.net 实例化对象

时间:2024-12-13 21:38:14

定义TestClass,在调用的地方

TestClass a;

如果下面有引用

a.Property,VS会报错,而如果

TestClass a = null;

再次调用的话则不会报错。

TestClass a; DebugIL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 0
.locals init ([0] class ConsoleApplication1.TestClass a)
IL_0000: nop
IL_0001: ret
} // end of method Program::Main

TestClass a = null; DebugIL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// Code size 4 (0x4)
.maxstack 1
.locals init ([0] class ConsoleApplication1.TestClass a)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
IL_0003: ret
} // end of method Program::Main

TestClass a; Release IL代码,VS会优化掉该语句

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method Program::Main

TestClass a = null; Release IL代码,VS同样会优化掉该句

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method Program::Main

先Mark,以后在具体研究区别