Small example of C delegating of delegate

  • 2020-05-10 18:39:18
  • OfStack

The code is as follows:


static void Main(string[] args)
        {
           Console.WriteLine(Exec(GetSet));
           Console.ReadKey();
        }
        // Defines a delegate to pass a method as a parameter Exec.
        public delegate string GetResultDelegate();
        public static string Get()
        {
            return "get";
        }
        public static string GetTest()
        {
            return "gettest";
        }
        public static string GetSet()
        {
            return "getSet";
        }

        public static string Exec(GetResultDelegate getResult)
        {
            return getResult();
 }

With the delegate, you can take one method as an argument to the other, and directly execute the argument OK. Similar to the function of javascript, the function in javascript can also be used as the parameter of another function. In javascript you can use the name of the function as an argument, but in c# you have to delegate, turn a corner, and do the same!


Related articles: