c#中如何把多个方法放入数组

2025-10-23 20:40:21

1、 // 1、定义一个委托 TestDelegate

        delegate void TestDelegate(string text);

2、设置好几个函数

static void MothedA(string text)

        {

            MessageBox.Show("方法A:" + text);

        }

        static void MothedB(string text)

        {

            MessageBox.Show("方法B:" + text);

        }

        static void MothedC(string text)

        {

            MessageBox.Show("方法C:" + text);

        }

3、public void Start()

        {

            //2、定义委托数组

            TestDelegate[] array = new TestDelegate[3];

            // 3、建立委托数字,将方法MothedA,MothedB,MothedC放入数组中

            array[0] = new TestDelegate(MothedA);

            array[1] = new TestDelegate(MothedB);

            array[2] = new TestDelegate(MothedC);

            // 4、从数组中就可以方便调用方法了

            array[0]("中国");

            array[1]("日本");

            array[2]("美国");

        }

c#中如何把多个方法放入数组

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢