-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local variable shadowing (CS0136) #8016
Comments
@orthoxerox Because it was decided during the design of C# 1.0 that said behavior is confusing and the cause of unexpected logic errors do to setting the wrong variable? I think it was a good idea then and you'll probably need a fantastic argument to demonstrate why the same potential problems don't still exist today. |
As long as it's C# it's no good, for example in Rust it's rather useful (to change mutable binding to immutable etc) although you can make it a warning, still, |
It would get complicated, mayge you could use some kind of keyword to go up a level, like the
|
@dan2002s And have, what, a parameter to specify the number of levels "up" you want to go? That sounds unnecessarily complicated, all to avoid having to think of a different identifier. |
I would prefer the exact opposite, that C# get rid of the ability to have overlapping names (such as using the same name for class member and a local variable or parameter). Unfortunately it would break BC to make that change now. |
I was assigned this ticket through codetriage. I believe that this should not be implemented. Can we close this issue as a won't fix? |
With introduction of pattern matching this feature would be really great. I hate I have to write code like var maybeSomething = foo?.Bar?.Baz;
if (maybeSomething is {} something1) { ... }
... // in multiple cases branches cannot be merged with single check
if (maybeSomething is {} something2) { ... }
...
if (maybeSomething is {} something3) { ... }
... Is this resolution final? It seems like introducing variable shadowing shouldn't be a breaking change since it relaxes rules rather than the other way around. |
Why not allow us to declare a local variable with the same name as a local variable in an enclosing block? For example,
would declare three separate variables. Perhaps they should be marked with an additional modifier to prevent accidental shadowing, like methods are?
The text was updated successfully, but these errors were encountered: