@onclick=“(() => SomeMethod(parameter))”

落爺英雄遲暮 提交于 2021-01-05 09:44:26

问题


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)"

回答1:


() =>() 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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions




回答2:


Use this pattern:

<input type="button" value="Click me" @onclick="@(() => SomeMethod("my string"))" />

when you want to call a method and pass it a parameter. Behind the scene, the compiler creates an EventCallback struct that provides the appropriate delegate type that encapsulates a method that has a single parameter and returns void

When you want to call a method that has no parameters use this pattern:

<input type="button" value="Click me" @onclick="@(SomeMethod1)" />

(not recommended, embedded in a Razor expression)

Or simply use the most common usage:

<input type="button" value="Click me" @onclick="@SomeMethod1" />

This is also possible:

<input type="button" value="Click me" @onclick="SomeMethod1" />

(The @ sign is not necessary here any longer, but you may see most developers use it though; out of habit, as this was necessary in the old days)

hope this helps...




回答3:


Option one, lambda function (anonymous function):

    @onclick="( ( ) => SomeMethod(parameter) ) "
       ^         ^     ^
       |         |     |
       (1)       (2)   (3)

When user clicks on control (1), the function (2) will be executed. (3) is the body of the function (2). This is an anonymous function, it has no name, because this only has parenthesys: __no_name_function__ ( ).

Option two, expression:

    @onclick="SomeMethod(parameter)"
       ^             ^
       |             |
       (1)           (2)

When user clicks on control (1), the result of expression (2) will be invoked. This function should return a delegate.

Sample

Play with this sample at blazorfiddle for a bit to understand it better.

<h1>@parameter</h1>

<button @onclick="@(() => SomeMethod1(parameter))" >button 1</button>

<button @onclick="SomeMethod2(parameter)" >button 2</button>

<p>@theResult</p>

@code
{
    int parameter = 5;
    string theResult = "";

    void SomeMethod1(int p)
    {
        parameter++;
        theResult = $"SomeMethod1 called p={p}";
    }

    Action SomeMethod2(int p)
    {        
        return SomeAction;
    }

    void SomeAction()
    {
        theResult = $"Action called parameter={parameter}";
        parameter++;
    }
}

Notice that is almost the same this:

<button @onclick="SomeMethod2(parameter)" >button 2</button>

than this:

<button @onclick="SomeAction" >button 2</button>


来源:https://stackoverflow.com/questions/58609923/onclick-somemethodparameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!