-
Is shadowing-in-nested-functions C# too free that people can reference outside var and define an new one with the same name in one method without any warn See the below code. Maybe it's difficult to debug and recognize there are 2 different var with the same name using System;
public class Class1
{
public void Test1(int val)
{
void Test2()
{
Console.WriteLine(val);//use var from outside scope
var array = new int[]{1};
foreach (var val in array)
{
Console.WriteLine(val);//use var from current scope
}
Console.WriteLine(val);//still use var from outside scope
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
We don't think so. |
Beta Was this translation helpful? Give feedback.
-
This was discussed in LDM on 11/03/2021. TLDR: we somewhat regret that you can both shadow and capture in the same local function, but we think it's not worth a warning wave or language change to correct at this point. |
Beta Was this translation helpful? Give feedback.
This was discussed in LDM on 11/03/2021. TLDR: we somewhat regret that you can both shadow and capture in the same local function, but we think it's not worth a warning wave or language change to correct at this point.