-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Improve usability when using uncaptured iteration variables in Blazor bindings #25252
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
Comments
Thanks for contacting us. |
We had a a contributor provide an analyzer for this, but we think this is better suited as part of Roslyn analyzers that isn't Blazor specific. Parking this in next sprint planning to figure out who gets to drive this work for the next release. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Marking as external as we have to work with |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
From my duplicate issue:
Having looked a bit further into the concept, I see that my suggestion would cause more trouble than it is worth. That being said, I think the
If I write a closure in C# code, it is much easier to notice this effect. Most C# developers are probably aware of the difference between Couldn't both the contributor's analyzer and the non-Blazor analyzer be integrated? The latter sounds like a different kind of warning to me, because "I wrote the closure, I know what I'm doing". |
This is going to be addressed as part of #44690.
|
Is your feature request related to a problem? Please describe.
When using the for-loop iterator variable as part of value binding in Blazor, you get an
IndexOutOfRange
exception. Consider this scenario:This appears to work at a first glance. However, as soon as you try to modify the bound string, you'll get a difficult to understand
IndexOutOfRange
exception:The problem is that the closure in the generated code doesn't use the
i
at iteration time, but the value ofi
long after the for-loop has ended, i.e.i
is not captured. The solution is to introduce a new local variable with a narrower scope:This is how C# works, but it is not very developer-friendly. It is quite difficult to debug this problem in Blazor, because the debugger will not let you look at the value of
i
. Instead you'll see that an exception was generated by some generated code, without line numbers and without context. Many people have struggled with problem before (e.g. #24460, #16815, #15633, #16144, #16073, #16809, #12539, #16276, many posts on StackOverflow and forums).Describe the solution you'd like
Given that it's not feasible to change the semantics of the language or include compiler warnings, the following alternatives might alleviate the problem:
i
leading to the exception.IndexOutOfRangeException
Perhaps there are other, much better/simpler solutions?
The text was updated successfully, but these errors were encountered: