-
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
Extract local function: Variable shadowing prior CSharp 8 #55135
Extract local function: Variable shadowing prior CSharp 8 #55135
Conversation
@CyrusNajmabadi What is the expected behavior: Name the function parameter
|
var f = ""{0}""; | ||
{|Rename:NewMethod|}(f); | ||
|
||
void NewMethod(string f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f
is causing CS0136 in C# 7.3. The refactoring should either name the parameter differently or capture f.
Capturing or renaming both seem reasonable. I'd like the refactoring to offer both options. |
#55446 has the fix to rename the variable here by default. If we want to capture as a different option we should file an issue for adding that |
#55446 uses the capturing approach. I'm going to close this PR in favor of #55446 because it is almost done and it fixes the issue. If anyone sees value in adding the "pass parameters (with renaming if before C# 8 or only offer passing if C# 8 and above)", that should be added after #55446 is merged. But I agree, that this is a separate issue. |
Closed in favor of #55446 |
Fix #55031
C# 8 allows shadowing of variables by local function parameters. The Extract local function refactoring takes this for granted. The following code is causing CS0136 (A local variable named 'var' cannot be declared in this scope because it would give a different meaning to 'var', which is already used in a 'parent or current/child' scope to denote something else) in C# 7.3 and older:
Before
After