C#委托浅谈

一、委托是什么?

    委托是引用类型,类似指针,指向一个方法,当调用委托的时候这个方法被立即执行

二、委托的声明

    1.自定义委托
    delegate 返回类型 委托名(参数);//其中参数是0个和多个,返回类型和参数必须与指向的方法一致
    2.微软官方定义的委托
    Action委托和Func委托,Action无返回值,Func带返回值,无论是Action还是Func委托最多只能有16个参数,Func委托有且仅有1个返回值

    简单应用如下:

		//简单委托
		delegate void HelloDelegate(string msg);//定义委托
		HelloDelegate helloDelegate = new HelloDelegate(Hello);//创建委托实例
		helloDelegate("您好,委托");//调用委托

        public void Hello(string str)
        {
            Console.WriteLine(str);
        }

		//Action委托
        Action action = new Action(Hello);
        Action();
        
        public void Hello()
        {
            Console.WriteLine("今天也要元气满满哦");
        }

		//Func委托	返回string类型,无参数
        Func<string> func = new Func<string>(Hello);
        func.Invoke();

        public string Hello()
        {
            return "今天也要元气满满哦";
        }

三、委托的执行

    一般通过 委托实例.Invoke() 方法或者 委托实例() 来执行委托指向的方法

四、泛型委托

    声明方式:
                    delegate 返回类型 GenericDelegate(T t);//T是任意类型
                    Action< T >;//T是任意类型
                    Func< T , R>;//T是参数类型,R是返回类型

五、多播委托

多播委托
1.每一个委托都继承自MulticastDelegate,也就是每个都是多播委托
2.带返回值的多播委托只返回最后一个方法的值
3.多播委托可以用加减+-号来操作方法的增加或减少
4.给委托传递相同方法时,生成的委托实例也是相同的(也就是同一委托)
           Action action = new Action(MethodTest);
           //将两个委托的调用列表连接在一起
           action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest1));
           action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest2));
           //从一个委托的调用列表中移除另一个委托的最后一个调用列表
           action = (Action)MulticastDelegate.Remove(action, new Action(MethodTest2));
           //打印的是  我是MethodTest()  我是MethodTest1()
           action();
            
           Action action = MethodTest;
           action += MethodTest1;
           action += MethodTest2;
           action -= MethodTest2;
           //打印的是  我是MethodTest()  我是MethodTest1()
           action();

           Func<string> func = new Func<string>(GetMethod);
           Console.WriteLine(func());
            
           Console.WriteLine("***************************************");
           //带返回值的多播委托只返回最后一个方法的值
           //匿名方法生成的是不同的方法
           Func<string> func = () => { return "我是Lambda"; };
           func += () => { return "我是func1"; };
           func += () => { return "我是func2"; };
           /*
           //打印的是我是func3
           func += () => { return "我是func3"; };
           func -= () => { return "我是func3"; };
           */
           //打印的是'我是func2'
           func += GetMethod;
           func -= GetMethod;

           Console.WriteLine(func());

------------------------------------------------------------------

	       public string GetMethod()
	       {
	           return "我是GetMethod()";
	       }
	
	       public void MethodTest2()
	       {
	           Console.WriteLine("我是MethodTest2()");
	       }
	
	       public void MethodTest1()
	       {
	           Console.WriteLine("我是MethodTest1()");
	       }
	
	       public void MethodTest()
	       {
	           Console.WriteLine("我是MethodTest()");
	       }

六、委托事件

事件的简单了解:事件就是委托的安全版本
第一点,在定义事件类的外部,它是不能使用=号来操作,只能用+=。
第二点,在定义事件类的外部不能调用事件。另外事件就是在委托的前面增加一个event关键字
	//Main方法
	#region EventDelegate
    Console.WriteLine("****************************委托事件****************************");
    EventDelegate eventDelegate = new EventDelegate();
    eventDelegate.StudentEvent += eventDelegate.Student;
    eventDelegate.StudentEvent += eventDelegate.Student1;
    eventDelegate.StudentEvent += eventDelegate.Student2;
    //打印	这是0号学员	这是1号学员	这是2号学员
    eventDelegate.Invoke();
    #endregion


	//EventDelegate类
    public delegate void StudentDelegate();
    public class EventDelegate
    {
        //定义事件
        public event StudentDelegate StudentEvent;

        public void Invoke()
        {
            StudentEvent?.Invoke();// ?.Null检查运算符
        }

        public void Student2()
        {
            Console.WriteLine("这是2号学员");
        }

        public void Student1()
        {
            Console.WriteLine("这是1号学员");
        }

        public void Student()
        {
            Console.WriteLine("这是0号学员");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值