c# 委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeleGateTest
{
    public delegate void GreetingDelegate(string _name);

    class Program
    {
        //public enum Language { English, Chinese }

        private static void EnglishGreeting(string _name)
        {
            Console.WriteLine("Good Morning!" + _name);
            Console.ReadLine();
        }

        private static void ChineseGreeting(string _name)
        {
            Console.WriteLine("早上好!" + _name);
            Console.ReadLine();
        }

        //public void GreetingPeople(string _name, Language language)
        //{
        //    switch (language)
        //    { 
        //        case Language.English:
        //            EnglishGreeting(_name);
        //            break;
        //        case Language.Chinese:
        //            ChineseGreeting(_name);
        //            break;
        //    }
        //}


        public static void GreetingPeopleTwo(string _name, GreetingDelegate greetingdelegate)
        {
            greetingdelegate(_name);
        }

        static void Main(string[] args)
        {
            //Program p = new Program();
            //p.GreetingPeople("jack",Language.English);
            //p.GreetingPeople("sidihu",Language.Chinese);

            
            //type one 
            //GreetingPeopleTwo("jack", EnglishGreeting);
            //GreetingPeopleTwo("jack", ChineseGreeting);

            //type two
            //string Cname = "";
            //string Ename = "jack";
            //GreetingDelegate GDEngilsh, GDchinese;
            //GDEngilsh = EnglishGreeting;
            //GDchinese = ChineseGreeting;
            //GreetingPeopleTwo(Cname,GDchinese);
            //GreetingPeopleTwo(Ename,GDEngilsh);


            //type three 可以将多个方法赋给同一个委托,或者叫将多个方法绑定到同一个委托,当调用这个委托的时候,将依次调用其所绑定的方法
            //GreetingDelegate GDall;
            //GDall = EnglishGreeting;
            //GDall += ChineseGreeting;
            //GreetingPeopleTwo("",GDall);

            //type four
            //GreetingDelegate GDall;
            //GDall = EnglishGreeting;
            //GDall += ChineseGreeting;
            //GDall("");

            //type five
            GreetingDelegate GDall = new GreetingDelegate(EnglishGreeting);
            GDall += ChineseGreeting;
            GDall("");
            GDall -= EnglishGreeting;//解除一个绑定
            GDall("sidihu");


        }
    }


    //public delegate void GreetingDelegate(string name);

    //class Program 
    //{   
    //    private static void EnglishGreeting(string name)
    //    {  
    //        Console.WriteLine("Morning, " + name);  
    //    }  
    //    private static void ChineseGreeting(string name)
    //    {  
    //        Console.WriteLine("早上好, " + name); 
    //    }  

    //    private static void GreetPeople(string name, GreetingDelegate MakeGreeting)
    //    {   
    //        MakeGreeting(name);  
    //    }   
        
    //    static void Main(string[] args)
    //    {   
    //        GreetPeople("Jimmy Zhang", EnglishGreeting); 
    //        GreetPeople("张子阳", ChineseGreeting); 
    //        Console.ReadKey();  
    //    }  
    //}  


}

 

posted @ 2012-05-23 20:54  sidihu  阅读(192)  评论(0编辑  收藏  举报