C#的委託案例

C#實現(Delegate)的委託就很少說了,直接上代碼,看代碼中的註釋:spa

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

    }
}
相關文章
相關標籤/搜索