why has x in the finally-block the value 1 instead of being "already
defined" or having the default value 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). 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.
What you have is different than
void foo () {
var x = 2;
if (true){
var x = 3;
}
}
Which isn't allowed.