Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@onclick="(() => SomeMethod(parameter))"

I am looking into Blazor and I stumbpled on this expression:

 @onclick="(() => SomeMethod(parameter))"

I cannot find/google anywhere what does this (I guess lambda) expression is actually doing. Can anybody explain me please this part: () => and why to use it and where?

EDIT:

What is the difference between the above and this:

 @onclick="SomeMethod(parameter)"
like image 515
JS_Diver Avatar asked Jun 07 '26 03:06

JS_Diver


1 Answers

() =>() is basically a lambda function.

Imagine you have a function

delegate (int foo) { return foo*2};

this can be rewritten as

(int foo)=>{return foo*2};

which can be shortened to

 foo=>foo*2;

Here your onlclick method executes SomeMethod which takes in a parameter

Why to use ? For creating simple and easy to use event handlers,callbacks delegates etc.

Reference

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions

like image 198
m5c Avatar answered Jun 10 '26 07:06

m5c



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!