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);
        }

    }
}

C#的委托案例

上一篇:转载:FFmpeg示例程序合集-跨平台批量编译脚本(MAC,WIN,Linux)


下一篇:[WPF 学习] 10.触发器