-
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
Issue: C# Assigning variable to inline delegate and using it in the delegate body causes "Using unassigned variable" error #15660
Comments
That is expected behavior. The variable has not been assigned yet. This applies to any expressions that attempt to reference the variable before it has been assigned, even if that variable will be assigned as a result of the expression: int x = x + 1; // CS0165: Use of unassigned local variable 'x' For delegates specifically, local functions alleviate the need for this as their hoisted scope allows them to be self-referential. |
|
The compiler does not track the lambda body as separate from the body of the current method, particularly in the case of a closure. In any case this feature is addressed by local functions which are being added to C#. |
Just found out how this feature is implemented in F#:
|
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! Note: this is doable with local-functions, and that woudl be the recommended approach here. |
Expected Behavior:
Actual Behavior:
Another good use case which is very common provided by @jnm2:
And combined with #15659 it could be something like this:
The text was updated successfully, but these errors were encountered: