Delegate Array in C#

前端 未结 5 1040
一整个雨季
一整个雨季 2021-02-14 18:10

I am experimenting with calling delegate functions from a delegate array. I\'ve been able to create the array of delegates, but how do I call the delegate?

publ         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 18:35

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pd[0](1);
            pd[1](2);
        }
    
        public delegate void delegates(int par);
        static delegates[] pd = new delegates[] 
                                         { 
                                          new delegates(MyClass.p1), 
                                          new delegates(MyClass.p2) 
                                         };
        public static class MyClass
        {
    
            public static void p1(int par)
            {
                MessageBox.Show(par.ToString());
            }
    
            public static void p2(int par)
            {
                MessageBox.Show(par.ToString());
            }
    
    
        }
    
    }
    

提交回复
热议问题