未分配的非空的变量值(C#) [英] Value of unassigned non-nullable variable (C#)
问题描述
只是好奇
如果你去:
string myString;
其值为NULL。
Its value is null.
不过,如果你去:
int myInt;
这是什么变量在C#中的价值?
What is the value of this variable in C#?
感谢
大卫
推荐答案
首先,请注意,这个只适用于域,而不是局部变量 - 那些不能被读取,直到他们已经被分配,至少在C#。事实上,CLR初始化栈帧为0,如果你有一个适当的标志集 - 我相信这是在默认情况下。这是很少观察到虽然 - 你必须通过一些难看黑客
Firstly, note that this is only applicable for fields, not local variables - those can't be read until they've been assigned, at least within C#. In fact the CLR initializes stack frames to 0 if you have an appropriate flag set - which I believe it is by default. It's rarely observable though - you have to go through some grotty hacks.
INT
的默认值是0 - 和任何类型的,它本质上是一个位模式完全零所代表的价值。对于一个值类型,这是调用参数的构造函数的等价物,并为引用类型,这是空。
The default value of int
is 0 - and for any type, it's essentially the value represented by a bit pattern full of zeroes. For a value type this is the equivalent of calling the parameterless constructor, and for a reference type this is null.
基本上CLR擦拭干净的内存用零。
Basically the CLR wipes the memory clean with zeroes.
这也是默认(SOMETYPE)值
的任何类型的。
这篇关于未分配的非空的变量值(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!