码迷,mamicode.com
首页 > Windows程序 > 详细

C#高级------委托

时间:2015-09-11 14:24:17      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

namespace out_ref
{
    //声明一个委托
    public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate mdl = Say;
            Do(mdl);
            Console.ReadKey();
        }

        static void Do(MyDelegate mdl)
        {
            mdl();
        }

        static void Say()
        {
            Console.WriteLine("哈哈");
        }
        
    }
}

 

namespace out_ref
{
    //声明一个委托
    public delegate int MyAdd(int n1,int n2);
    class Program
    {
        static void Main(string[] args)
        {
            Do(Sum);
            Console.ReadKey();
        }

        static void Do(MyAdd myadd)
        {
            int sum = myadd(1,3);
            Console.WriteLine(sum);
        }

        static int Sum(int n1,int n2)
        {
            return n1 + n2;
        }
      
        
    }
}
namespace out_ref
{
   //委托占位案例
    public delegate void Myadd();
    class Program
    {
        static void Main(string[] args)
        {
            Do(Dafeiji);
            Console.ReadKey();
        }

        static void Do(Myadd myadd)
        {
            Console.WriteLine("我正在吃饭....");
            myadd();
            Console.WriteLine("我正在睡觉....");
        }

        static void Play()
        {
            Console.WriteLine("我在玩游戏....");
        }

        static void Dafeiji()
        {
            Console.WriteLine("我正在打飞机....");
        }
       
    }
}

 

C#高级------委托

标签:

原文地址:http://www.cnblogs.com/phpweige/p/4800549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!