C#的委托案例

C#实现(Delegate)的委托就不多说了,直接上代码,看代码中的注释:

namespace Delegate
{
    delegate void DGSayiHi(string name);//声明委托
    delegate void DGDo(string name);
    class Program
    {
        static void Main(string[] args)
        {
            DGSayiHi sayhi = new DGSayiHi(SayChineseHi);
            DGSayiHi sayhi2 = new DGSayiHi(SayEnglishHi);
            sayhi += sayhi2;//实际上+=相当于Delegate.Combine, -=相当于Delegate.Remove

            sayhi("春晓");
            Console.ReadLine();
        }
        static void SayChineseHi(string name)
        {
            Console.WriteLine("你好:" + name);
        }

        static void SayEnglishHi(string name)
        {
            Console.WriteLine("Hello:" + name);
        }

    }
}
posted @ 2020-04-21 20:21  moon3  阅读(317)  评论(0编辑  收藏  举报