Pass a method as an argument

后端 未结 5 1590
离开以前
离开以前 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:48

    protected delegate String MyDelegate(String str);
    
    protected void MyMethod()
    {
        MyDelegate del1 = new MyDelegate(ParamMethod);
        RunMethod(del1, "World");
    }
    
    protected void RunMethod(MyDelegate mydelegate, String s)
    {
        MessageBox.Show(mydelegate(s) );
    }
    
    protected String ParamMethod(String sWho)
    {
        return "Hello " + sWho;
    }
    

提交回复
热议问题