Pass Method as Parameter using C#

后端 未结 12 1531
不知归路
不知归路 2020-11-21 23:30

I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method tha

12条回答
  •  滥情空心
    2020-11-22 00:13

    If you want to pass Method as parameter, use:

    using System;
    
    public void Method1()
    {
        CallingMethod(CalledMethod);
    }
    
    public void CallingMethod(Action method)
    {
        method();   // This will call the method that has been passed as parameter
    }
    
    public void CalledMethod()
    {
        Console.WriteLine("This method is called by passing parameter");
    }
    

提交回复
热议问题