-
Notifications
You must be signed in to change notification settings - Fork 126
Explain the difference between for and foreach loop in context of lambda expression #371
Comments
Here is yet another example with binding expressions. <div>List A</div>
@for (var i = 0; i < customers.Length - 1; i++)
{
// This binding expression doesn't work correctly.
<div><input bind="@customers[i].Name" /></div>
}
<br />
<div>List B</div>
@for (var i = 0; i < customers.Length; i++)
{
int localVar = i;
<div><input bind="@customers[localVar].Name" /></div>
}
<br />
<div>List C</div>
@foreach (var cust in customers)
{
<div><input bind="@cust.Name" /></div>
} @functions{
public class Customer
{
public string Name { get; set; }
};
Customer[] customers = new Customer[]
{
new Customer { Name = "Cust A" },
new Customer { Name = "Cust B" },
new Customer { Name = "Cust C" },
new Customer { Name = "Last customer" }
};
} Probably the best place to add this example is here https://blazor.net/docs/components/index.html#data-binding a little above event handling. Eventually here https://blazor.net/docs/tutorials/build-your-first-blazor-app.html#build-a-todo-list in step number 17 we can add warning that if someone wants to use |
Thanks for opening this issue @Andrzej-W. Your points and explanation are excellent. Before we discuss what to do in the Blazor docs, let's call in the TOP GUN in C# for advice ... @BillWagner 👋 TL;DR Devs are getting tripped up using I'd like to link to C# Guide content (or other authoritative content) and provide as little original content in the Blazor docs as possible on these points. I took a look at ... I don't see the conceptual difference that we'd like to highlight for this scenario laid out plainly ... not in a form that we can link, such as a specific section that goes directly to these points. What's your advice, and are you aware of content that we can link in a MS doc set (or perhaps another trusted doc set)? |
@guardrex , @BillWagner I have found this "trusted doc":
You can link to this doc but good examples as demonstrated above are necessary. |
Issue has been moved to aspnet/Docs. |
In the current doc we have plenty of examples with
foreach
loops, but people often usefor
loops and have problems. Although it is a general C# topic and not Blazor specific, we should add some info and explain the difference. Otherwise we will have more "bug reports" like these: https://github.com/aspnet/Blazor/issues/665, https://github.com/aspnet/Blazor/issues/764, https://github.com/aspnet/Blazor/issues/1402, dotnet/aspnetcore#6591.Notice that I selected only a few examples and there are a lot of similar issues.
Problem is typically seen in event handlers and binding expressions. We should explain that in
for
loop we have only one iteration variable and inforeach
we have a new variable for every iteration. We should explain that HTML content is rendered whenfor
/foreach
loop is executed, but event handlers are called later. Here is an example code to demonstrate one wrong and two good solutions.Unfortunately English is not my native language and probably I'm not the proper person to write this.
I'm also not sure where to add this information, maybe here: https://blazor.net/docs/components/index.html#event-handling
The text was updated successfully, but these errors were encountered: