首页 > 其他 > 详细

委托 delegate

时间:2017-02-18 00:38:41      阅读:154      评论:0      收藏:0      [点我收藏+]
public delegate string MyDelegate(int num1);

class Program{

static void Main(string[] args){

 int t1 = Test1(Show);

Program1 p1 = new Program1();
int t2 = Test1(p1.Display); }
public static string Show(int num){ return "人数为:"+ num; } public static int Test1(MyDelegate myDel){ string str = myDel(3);   return str.Length; } }


public class Program1{
  public string Display(int a){
  return "车辆数为:"+ a;
  }
}

 

 

实现加减乘除

加减乘除时 统一调用方法 calc

public delegate int MyDelegate(int a, int b);

int
calc(int a, int b, MyDeletgate operation){ return operation(a,b); } int add(int a, int b){ return a+b; } int minus(int a, int b){ return a-b; } int plus(int a ,int b ){ return a*b; } int divide(int a, int b){   return a/b; }

 

 

public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();

            DoSth(mc.DoTime);
            //DoSth(WriteTime);
            Console.ReadKey();
        }
        public static void DoSth(MyDelegate mdl)
        {
            Console.WriteLine("吃早饭");
            Console.WriteLine("读书");
            Console.WriteLine("飞流直下三千尺");
            Console.WriteLine("疑是银河落九天");
            //做事 不知道做什么
            mdl();
           // Console.WriteLine(System.DateTime.Now.ToString());
            //File.WriteAllText("1.txt",System.DateTime.Now.ToString());
            Console.WriteLine("吃饭");

        }
        public static void ShowTime()
        {
            Console.WriteLine(System.DateTime.Now.ToString());
        }

        public static void WriteTime()
        {
            File.WriteAllText("1.txt",System.DateTime.Now.ToString());
        }

 

 

笔记:

MyDelegate是委托 , 委托是一种特殊的类型 ,委托的实例为一个方法。委托 可以把方法作为参数传递。

委托 delegate

原文:http://www.cnblogs.com/rockywood/p/6411851.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!