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)"
() =>() 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With