Pass Method as Parameter using C#

后端 未结 12 1367
不知归路
不知归路 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:11

    class PersonDB
    {
      string[] list = { "John", "Sam", "Dave" };
      public void Process(ProcessPersonDelegate f)
      {
        foreach(string s in list) f(s);
      }
    }
    

    The second class is Client, which will use the storage class. It has a Main method that creates an instance of PersonDB, and it calls that object’s Process method with a method that is defined in the Client class.

    class Client
    {
      static void Main()
      {
        PersonDB p = new PersonDB();
        p.Process(PrintName);
      }
      static void PrintName(string name)
      {
        System.Console.WriteLine(name);
      }
    }
    

提交回复
热议问题