//Consider this declaration
string name;
Here string variable name is an unassigned variable,does this declaration reserve any memory for name if it does not initialised?
//Consider this declaration
string name;
Here string variable name is an unassigned variable,does this declaration reserve any memory for name if it does not initialised?
它不是未分配的。所有类/结构都接收它们的默认值。对于字符串,它是null
.
如果它是一个局部变量,那么优化将倾向于删除它。如果它是一个实例变量,那么将分配内存(我认为,C# 规范尚不清楚)。
方法的局部变量不保留任何内存,寄存器分配给它取决于它的使用方式和其他变量的使用方式。只要它不被使用,就不会为其分配寄存器。
您可以在方法中包含大量变量,但 CPU 中的寄存器数量有限,因此编译器会优化您的代码以重用寄存器。有关详细信息,请参阅寄存器分配。
不,string name;
不保留任何内存。