== Why is this an issue?
In Blazor, using https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling#lambda-expressions[lambda expressions] as https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling#lambda-expressions[event handlers] when the UI elements are rendered in a loop can lead to negative user experiences and performance issues. This is particularly noticeable when rendering a large number of elements.
The reason behind this is that Blazor rebuilds all lambda expressions within the loop every time the UI elements are rendered.
== How to fix it
Ensure to not use a delegate in elements rendered in loops, you can try:
* using a collection of objects containing the delegate as an https://learn.microsoft.com/en-us/dotnet/api/system.action[Action],
* or extracting the elements into a dedicated component and using an https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling#eventcallback[EventCallback] to call the delegate
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
@for (var i = 1; i < 100; i++)
{
var buttonNumber = i;
}
@code {
private void DoAction(MouseEventArgs e, int button)
{
// Do something here
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
@foreach (var button in Buttons)
{
}
@code {
private List