简体   繁体   English

最后尝试C#的变量生命周期

[英]C# lifetime of a variable in try finally

i got the following code 我得到了以下代码

截图

why has x in the finally-block the value 5 instead of beeing "already defined" or having the default value 0? 为什么在finally块中的x值为5而不是“已经定义”或者默认值为0?

The lifetime of x is within {...} block, try in your case; x的生命周期在{...}块内, try在你的情况下; however, since there's no zero-initialization of local variables in .Net the next x contains trash which is former x value 但是,由于.Net中的局部变量没有零初始化 ,下一个x包含前x值的垃圾

try
{
   int x = 5;
}
finally
{
   // x is re-declared; since x is local it contains trash;
   // former x was aquired at the stack, so .Net just moves stack pointer 
   // and doesn't remove former x value
   int x; 
   ...
}

http://msdn.microsoft.com/en-us/library/aa691170(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/aa691170(v=vs.71).aspx

...A local variable is not automatically initialized and thus has no default value ... ...局部变量不会自动初始化 ,因此没有默认值 ...

http://msdn.microsoft.com/en-us/library/aa664742(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/aa664742(v=vs.71).aspx

...The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs ... ...在local-variable-declaration中声明的局部变量的范围是声明发生的块 ...

I guess you have set a breakpoint in the finally and looked at x . 我猜你已经在finally设置了断点并查看了x x has no value according to the C# language spec but the debugger probably looked at the storage location that the first x had and showed you its value. 根据C#语言规范, x没有值,但调试器可能会查看第一个x具有的存储位置并向您显示其值。

In real code you would be unable to read from x in the finally . 在实际代码中,您将无法在finally读取x

The debugger does not obey the rules of the language. 调试器不遵守语言规则。

可能会使用visual studio调试器,它会在值变量名称上传递值,而在某些混乱情况下会显示错误值。

The scope of variable x is limited to try block only, yes Debugger shows the value 5 but if you try to use x variable you will face the problems you mentioned. 变量x的范围仅限于try block,yes Debugger显示值5但是如果你尝试使用x变量,你将面临你提到的问题。

Try This: 尝试这个:

finally
{
   int x;
   Console.Write(x); //error use of unassigned local variable
}

why has x in the finally-block the value 1 instead of being "already defined" or having the default value 0? 为什么在finally块中的x值为1而不是“已定义”或默认值为0?

What you are seeing is the debugger linking up the symbols. 您所看到的是连接符号的调试器。 It doesn't have a value at all, and in fact if you try and use it you get an unassigned variable error. 它根本没有值,事实上如果你尝试使用它,你会得到一个未分配的变量错误。

Now why doesn't it show as already defined is another question. 现在为什么它没有显示已经定义的另一个问题。 The answer is that the {} define the declaration space. 答案是{}定义声明空间。 (In C# variables are defined at the branch level, not the function level). (在C#变量中,在分支级别定义,而不是在函数级别定义)。 They are in two different declaration spaces and that's why it is allowed. 它们位于两个不同的声明空间中,这就是它被允许的原因。 The first x can't spill into where the second x is defined. 第一个x无法溢出到定义第二个x位置。

What you have is different than 你拥有的不同于

void foo () {
 var x = 2;

 if (true){
   var x = 3;
 }

}

Which isn't allowed. 这是不允许的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM