Delegate Array in C#

前端 未结 5 1047
一整个雨季
一整个雨季 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:41

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

提交回复
热议问题