C#中的委托(Delegate)

什么是委托?

首先,我们要知道C#是一种强类型的编程语言,强类型的编程语言的特性,是所有的东西都是特定的类型

委托 是一种存储函数的引用类型,就像我们定义的一个 string str 一样,这个str 变量就是 string类型. 因为C#中没有函数类型,但是可以定义一个委托类型,把这个函数赋值给这个委托

cs 复制代码
 //声明委托
 //delegate 返回值类型 委托的名字(方法的参数) 

   delegate <return type> <delegate-name><parameter list>

 //声明一个委托,接收string参数,返回值为int类型 
  
 pubulic delegate int MyDelegate(string s)

委托一旦被声明,就可以用new关键字来创建声明委托

委托的使用

cs 复制代码
方法一:

  public delegate int MyDelegate(string s);
  internal class Program
  {
      static void Main(string[] args)
      {
          MyDelegate d1 = new MyDelegate(Number);
          d1("11");
      }
      static int Number(string b)
      {
          Console.WriteLine("你好"+b);
          return 1;
      }
  }

方法二: 
   
  public delegate int MDelegate(string a);
  internal class Program
  {
      static void Main(string[] args)
      {
         Test.TestT(Num);
      }
      static int Num(string str)
      {
          Console.WriteLine("你好"+str);
          return 1;
      }
  }
 class Test
 {
     public static void TestT(MDelegate aa)
     {
         //方法接收一个委托类型的参数,就相当于接收了一个方法,该方法必须满足这个委托的规定的参数和返回值
         //aa 回调函数:以参数的形式传递到函数中的函数
         aa("12");
     }
 }

实例化委托

cs 复制代码
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托实例化
{
    delegate void Mystring(int x, string y);
    delegate int NumberOper(int aa, int bb);
    internal class Program
    {
        static void Main(string[] args)
        {
            new Test();
            Mystring mystring=new Mystring(Fn);
            mystring(1, "aaa");
        }
        static void Fn(int x, string y)
        {
            Console.WriteLine("Helloween");
        }
    }
    class Test
    {
        public Test()
        {
            string str = "2025";
            Mystring mystring=new Mystring(Fn);
            mystring(1,str);

            NumberOper sum = new NumberOper(Add);
            Console.WriteLine(Add(20,30));
        }
        void Fn(int x, string y)
        {
            Console.WriteLine($"x==={x},y==={y}");
        }
        int Add(int a, int b)
        {
            return a + b;
        }
    }
}

什么是多播委托?

一个委托可以引用多个方法,可以依次调用所有引用的方法。可以通过使用*+* 运算符来合并委托或使用*-*运算符来移除特定的方法实现。

cs 复制代码
 delegate void MyDelegate(string name);
 internal class Program
 {
     static void Main(string[] args)
     {
         //包含多个方法的委托,称之为多播委托
         MyDelegate fns = new MyDelegate(Fn1);
         //使用+=运算符, 再委托变量上再次添加一个方法
         fns += new MyDelegate(new Test().Fn2);
     }
     public static void Fn1(string a)
     {
         Console.WriteLine($"这是Fn1中的a==={a}");
     }
 }
 class Test
 {
     public void Fn2(string x)
     {
         Console.WriteLine($"这是Fn2中的a==={x}");
     }
     public static void Fn3(string x)
     {
         Console.WriteLine($"这是Fn3中的a==={x}");
     }
 }

多波委托你也可以理解为捆绑事件,一个按钮绑定了多个功能

例如:

C# Winform 全选/反选(CheckBox)控件-CSDN博客

相关推荐
mudtools15 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下20 小时前
最终的信号类
开发语言·c++·算法
echoarts20 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix21 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz21 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题21 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说21 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号1 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_1 天前
QT(4)
开发语言·汇编·c++·qt·算法