How to store delegates in a List

后端 未结 4 1169
灰色年华
灰色年华 2020-12-11 01:19

How can I store delegates (named, anonymous, lambda) in a generic list? Basically I am trying to build a delegate dictionary from where I can access a stored delegate using

4条回答
  •  隐瞒了意图╮
    2020-12-11 02:08

    Well, here's a simple example:

    class Program
    {
        public delegate double MethodDelegate( double a );
    
        static void Main()
        {
            var delList = new List {Foo, FooBar};
    
    
            Console.WriteLine(delList[0](12.34));
            Console.WriteLine(delList[1](16.34));
    
            Console.ReadLine();
        }
    
        private static double Foo(double a)
        {
            return Math.Round(a);
        }
    
        private static double FooBar(double a)
        {
            return Math.Round(a);
        }
    }
    

提交回复
热议问题