Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor difference between a lambda callback or a direct callback [closed]

So I was experimenting with Blazor and I'd like to understand the difference between these two.

<p @onclick="OnClickCallback">Click me normal</p>

<p @onclick="async () => await OnClickCallback()">Click me lambda</p>

@code {

  private async Task OnClickCallback()
  {
    await Task.Delay(500);
  }

}

What is the difference between these two approaches. What happens behind the scenes. I've found some issues with EF Core (which is a whole other topic) where the lambda approach did not throw an exception, and the normal did.

like image 480
Roy Berris Avatar asked Mar 11 '26 11:03

Roy Berris


1 Answers

What is the difference between these two approaches

It adds another function, which means it introduces another call on the stack.

Also, because this is an async lambda, it allocates another Task instance.

This being said, using the former will have a negligible performance benefit.


For completeness, there is a third alternative that would prevent another Task being used, but would still result in another function call:

() => OnClickCallback()
like image 52
Johnathan Barclay Avatar answered Mar 13 '26 23:03

Johnathan Barclay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!