Skip to content
Discussion options

You must be logged in to vote

This would be a non-breaking change

It is a breaking change that in order to keep scoping rules consistent it would be illegal to declare a new variable of the same name in the finally block. This is legal today:

try {
    int x = CalculateX();
}
finally {
    int x = SomeOtherCalculation();
}

Also, as @YairHalberstadt points out, given the exception could be thrown at nearly any point in the try block the variable would remain unassigned as far as the finally block is concerned. You can see this today by lifting the variable out of the try scope to the parent scope:

int x;
try {
    x = CalculateX();
}
finally {
    Console.WriteLine(x); // error CS0165: Use of unassigned local variabl…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YairHalberstadt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2213 on October 21, 2020 13:09.