C# 四十二、delegate(委托)

概念

委托也叫代理,就是把事情交付给别人去办。

委托是具有相同签名和返回值类型的有序方法列表。

委托定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。

委托是一种引用类型。

方法的列表称为调用列表,当委托被调用时,它调用列表中的每个方法。

委托是方法的抽象,它存储的就是一系列具有相同签名和返回回类型的方法的地址/引用。

定义委托

语法格式:访问修饰符 delegate 返回类型 标识符(参数列表);

委托的实例与赋值 

声明实例的语法格式:委托类型 标识符;

对委托赋值语法格式:标识符=方法;

多播委托:

    标识符+=方法(向委托上绑定方法)

    标识符-=方法(解除委托上的方法)

注意事项

 1、委托和要代理的方法必须有相同的返回类型和参数

2、委托调用过程中的协变与抗变

  • 协变是指能够使用与原始指定的派生类型相比,派生程度更大的类型。
    • 返回类型需要注意的是协变,方法的返回类型可以派生于委托定义的类型。
  • 抗变/逆变则是指能够使用派生程度更小的类型。
    • 参数类型需要注意的是抗变,向委托传递的参数类型可以派生于委托方法的参数类型。

3、命名规范:委托标识符以EventHandle结尾。

委托实现

 一、声明委托

二、声明对象

三、绑定方法

四、调用委托

 

代码示例一:

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

namespace Test_CSharp
{
    class Program
    {
        delegate void myEventHandle();//声明委托
        static void Main(string[] args)
        {
            myEventHandle myDelegate;//声明实例
            myDelegate = Function;//绑定方法
            //myDelegate = new myEventHandle(Function);
            myDelegate();//调用委托
           Console.ReadKey();
        }

        static void Function()//被代理方法
        {
            Console.WriteLine("OK");
        }
    }
}
--->
OK

代码示例二:

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

namespace Test_CSharp
{
    public delegate void myEventHandler();//声明委托

    class Program
    {
        static void Main(string[] args)
        {
            FunctionA(FunctionB);//传参时绑定方法(使方法作为参数传递)

            Console.ReadKey();
        }

        static void FunctionA(myEventHandler myDelegate)//声明实例(作为参数)
        {
            myDelegate();//调用委托
            Console.WriteLine("回家吃饭");
        }

        static void FunctionB()
        {
            Console.WriteLine("你妈喊你");
        }
    }
}

--->
你妈喊你
回家吃饭

示例三:

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

namespace Test_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //类的实例
            Class1 class1 = new Class1();
            Class2 class2 = new Class2();
            class2.myDelegate += class1.FunctionA;//绑定方法
            class2.myDelegate += class1.FunctionB;
            class2.myDelegate += class1.FunctionC;
            class2.myDelegate("好");//调用委托

            Console.ReadKey();
        }
    }

    class Class1
    {
        public void FunctionA(string name)
        {
            Console.WriteLine("你{0}", name);
        }
        public void FunctionB(string name)
        {
            Console.WriteLine("我{0}", name);
        }
        public void FunctionC(string name)
        {
            Console.WriteLine("大家{0}", name);
        }
    }

    class Class2
    {
        public delegate void MyEvevtHandler(string name);//声明委托
        public MyEvevtHandler myDelegate;//声明实例
    }
}

--->
你好
我好
大家好

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值