C#变量的初始化问题 [英] C# Variable Initialization Question
本文介绍了C#变量的初始化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有我是否喜欢初始化一个整数变量任何区别:
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)和其真正的,只有INT设置为0,真是设置为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;
}
生成:
Generates:
.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#变量的初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文