C#之委托

    1.委托,实例化类,对象调用方法    

public class MydelegateDemo    

{        

        public delegate void Mydelegate(string name);//声明委托

        public void DelegateFunc(string name)//我们欲传递的方法        

       {            

             Console.WriteLine("Hello,{0}", name);        

       }

        public static void Main(string[] args)        

        {            

               MydelegateDemo mdd = new MydelegateDemo();            

               Mydelegate md = new Mydelegate(mdd.DelegateFunc);            

               md("Lobin");            

               Console.ReadKey();        

        }

 }

    2.委托,静态方法,类名调用方法    

public class MydelegateDemo2    

{        

        public delegate void Mydelegate(string name);//声明委托

        public static void DelegateFunc(string name)        

       {            

              Console.WriteLine("How are you? {0}", name);     

       }

        public static void Main(string[] args)       

       {            

             Mydelegate md = new Mydelegate(MydelegateDemo2.DelegateFunc);        

              md("Lobin");            

              Console.ReadKey();       

       }    

}

posted on 2016-03-04 16:24  可爱的春哥  阅读(120)  评论(0编辑  收藏  举报

导航