我是否初始化一个整数变量有什么区别:
Is there any difference on whether I initialize an integer variable like:
int i = 0;
int i;
编译器或 CLR 是否将其视为同一件事?IIRC,我认为它们都被视为同一个东西,但我似乎找不到这篇文章.
Does the compiler or CLR treat this as the same thing? IIRC, I think they're both treated as the same thing, but I can't seem to find the article.
我查看了IL(使用ildasm),确实只有设置为0的int在构造函数中真正设置为0.
I looked at the IL (using ildasm) and its true that only the int set to 0 is really set to 0 in the constructor.
public class Class1
{
int setToZero = 0;
int notSet;
}
生成:
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.0
IL_0002: stfld int32 ClassLibrary1.Class1::setToZero
IL_0007: ldarg.0
IL_0008: call instance void [mscorlib]System.Object::.ctor()
IL_000d: nop
IL_000e: ret
} // end of method Class1::.ctor
这篇关于C# 变量初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!