Pass a method as an argument

后端 未结 5 1537
离开以前
离开以前 2020-12-31 20:12

How do I pass a method as an argument? I do this all the time in Javascript and need to use anonymous methods to pass params. How do I do it in c#?

protecte         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 20:40

    protected String ParamMethod(String sWho)
    {
        return "Hello " + sWho;
    }
    
    protected void RunMethod(Func ArgMethod)
    {
        MessageBox.Show(ArgMethod());
    }
    
    protected void MyMethod()
    {
        RunMethod( () => ParamMethod("World"));
    }
    

    That () => is important. It creates an anonymous Func from the Func that is ParamMethod.

提交回复
热议问题