C# --- 委托机制 delegate 和 回调方法 callback

本文详细介绍了C#中的委托机制,包括单播和多播委托的使用示例,以及委托如何实现解耦。此外,还探讨了Action和Func这两个预定义的委托类型在简化代码中的作用。同时,文章讨论了回调的概念,解释了为何需要回调函数,并提到了回调在事件处理中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是委托机制

  • 委托机制相当于C语言中的函数指针, 将一个方法的reference传入另外一个方法中

Example

//创建一个方法 
//创建一个委托
//这个委托的返回值是void并且有一个参数是string. 也就是这个委托可以指向任何 **返回值为void参数为string的方法**
//使用这个委托, 将要指向的方法名传入委托的constructor
public static void PrintMessage(string msg)
{
	Console.WriteLine(msg)
}
public delegate void PrintMsgFunctionDelgate(string Message);
PrintMsgFunctionDelegate print = new PrintMsgFunctionDelegate(PrintMessage)
print("welcome")

Unicast deligate

  • This delegate refers to only one method, as shown in the above examples.

Multicast deligate

  • This delegate can refer to more than one method. This delegate maintains a list of the methods.
//Example 1
delegate void strDele(string str); //declare  
strDele delobj += new strDele (uppercasestr); //method reference 1  
delobj += new strDele (Lowercasestr); //method reference 2   
delobj(“Welcome”); //invoking the multicast delegate 
//Example 2
using System;

delegate int NumberChanger(int n);
namespace DelegateAppl
{
   class TestDelegate
   {
      static int num = 10;
      public static int AddNum(int p)
      {
         num += p;
         return num;
      }

      public static int MultNum(int q)
      {
         num *= q;
         return num;
      }
      public static int getNum()
      {
         return num;
      }

      static void Main(string[] args)
      {
         // 创建委托实例
         NumberChanger nc;
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         nc = nc1;
         nc += nc2;
         // 调用多播
         nc(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}
Output:
Value of Num: 75

委托机制的优点

  • 使用委托可以达到解耦, 如下, 创建一个方法, 用来显示可以升职的员工
class Employee  
{  
   public int ID { get; set; }  
    public string Name { get; set; }  
    public int salary { get; set; }  
    public float Experiance { get; set; }  

    public static void PromoteEmp(List<Employee> EmployeeList)  
    {  
        foreach (Employee  emp in EmployeeList )  
        {  
            if(emp.Experiance>=5)//logic condition  
            {  
                Console.WriteLine(emp.Name + " promoted");  
            }  
        }  
    }  
}  
  • 目前的筛选机制是工作经验大于五年的可以升职, 但是如果需要改变计算规则, 比如变成工资大于10000的可以升职, 就需要改动代码. 而使用委托就可以达到解耦
//将计算规则封装成不同的方法
Public static bool promoteByYOE(Employee emp)  
{  
     if (emp.Experiance >= 5)  
     {  
         return true;  
     }  
     else  
     {  
         return false;  
     }  
}  

Public static bool promoteBySalary(Employee emp)  
{  
     if (emp.Salary >= 10000)  
     {  
         return true;  
     }  
     else  
     {  
         return false;  
     }  
}  

//将计算规则用委托代表, 命名为IsEligible
public static void PromoteEmp(List<Employee> EmployeeList,isPromote IsEligible)  
{  
     foreach (Employee  emp in EmployeeList )  
     {  
         if(IsEligible(emp))//logic condition  
         {  
             Console.WriteLine(emp.Name + " Promoted");  
         }  
     }  
}

//创建一个委托, 返回类型为bool, 参数为Employee
delegate bool isPromote(Employee emp); 
//让委托指向不同的方法
IsPromote proByEOY = new isPromote(promoteByYOE)
IsPromote proBySalary = new isPromote(promoteBySalary)

Employee.PromoteEmp(empl, proByEOY);
Employee.PromoteEmp(empl, proBySalary);

C# 中的Action 和 Func

Action

  • 对于正常的委托, 每次都要定义, 比较麻烦, 而使用Action可以省去定义委托的过程
  • 使用格式为
    Action<parameter type1, parameter type2 ... > delegate_name = new Action <parameter type1, parameter type2 ... >(method name)
  • Action只适用于没有返回值的方法
public static void PrintMessage(string msg)
{
	Console.WriteLine(msg)
}
//正常定义委托
public delegate void PrintMsgFunctionDelgate(string Message);
PrintMsgFunctionDelegate print = new PrintMsgFunctionDelegate(PrintMessage)
print("welcome")

//使用Action定义委托, 可以使用new关键字 或者直接赋值
Action<string> print() = new Action<string>(PrintMessage);
Action<string> print() = PrintMessage;
print("welcome");
//使用Action + 匿名方法
static void Main(string[] args)
{
    Action<int> printActionDel = delegate(int i)
                                {
                                    Console.WriteLine(i);
                                };

    printActionDel(10);
}

//使用Lambda + Action
static void Main(string[] args)
{

    Action<int> printActionDel = i => Console.WriteLine(i);
       
    printActionDel(10);
}

Func

  • Func适用于有返回值的方法, signature如下
    在这里插入图片描述
namespace System
{  
    //第一个参数是input, 最后一个是返回值类型
    public delegate TResult Func<in T, out TResult>(T arg);
}
  • The following Func delegate takes two input parameters of int type and returns a value of int type: Func<int, int, int> sum;
  • Func with Zero Input Parameter: Func<int> getRandomNumber;

Example:

class Program
{
    static int Sum(int x, int y)
    {
        return x + y;
    }

    static void Main(string[] args)
    {
        Func<int,int, int> add = Sum;

        int result = add(10, 10);

        Console.WriteLine(result); 
    }
}
//Func with an Anonymous Method
Func<int> getRandomNumber = delegate()
                            {
                                Random rnd = new Random();
                                return rnd.Next(1, 100);
                            };
//Example: Func with lambda expression
Func<int> getRandomNumber = () => new Random().Next(1, 100);

//Or 

Func<int, int, int>  Sum  = (x, y) => x + y;

使用lambda表达式定义Func和Action委托

//Example 1: Action委托
Action<string> greet = name =>
{
    string greeting = $"Hello {name}!";
    Console.WriteLine(greeting);
};
greet("World");
// Output:
// Hello World!
//Example 2: Func委托
Func<int, int> square = x => x * x;
Console.WriteLine(square(5));
// Output:
// 25

Func<int, int, bool> testForEquality = (x, y) => x == y;
Console.WriteLine(testForEquality(5, 7));
// Output:
// False

Func<int, string, bool> isTooLong = (x, s) => s.Length > x;
Console.WriteLine(testForEquality(3, "hello"));
// Output:
// True
//Example 3: Natural type of a lambda expression
//从C# 10开始, lambda expression不需要必须定义成一种委托类型, 比如`Func` 或者 `Action`.
//而是可以定义成var类型, 然后由编译器自己推断类型.
//会自动转换成 Func<string, int> 类型
var parse = (string s) => int.Parse(s);


//会自动转换成 Func<int, int, int> 类型
var IncrementBy = (int source, int increment = 1) => source + increment;
Console.WriteLine(IncrementBy(5)); // 6
Console.WriteLine(IncrementBy(5, 2)); // 7


//但是必须要有足够的信息才能自动转换
var parse = s => int.Parse(s); // ERROR: Not enough type info in the lambda

委托机制的主要用处 — 回调 Callback

  • 回调的定义
  • 回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数
  • 为什么需要将函数的指针当参数传进去, 而不是直接调用需要的函数
  • “你想让别人的代码执行你的代码,而别人的代码你又不能动”。所以你要用别人的函数通过指针的形式间接调用你的函数,即回调函数"
  • 也就是调用我写的函数的人, 可以将任何参数和返回值符合要求的函数传进我写的函数, 然后被我写的函数调用, 而不需要改动我的代码
  • 回调最常用的地方是event handler, 也就是根据鼠标或者键盘的event执行不同的函数
  • 回调有两种实现方式: 通过委托或者接口实现

通过委托实现

public delegate void TaskCompletedCallBack(string taskResult);

public class CallBack
{
    public void callBackFunc(string result)
    {
        Console.WriteLine(result);
    }
	
	public void StartNewTask(TaskCompletedCallBack taskCompletedCallBack)
    {
        Console.WriteLine("I have started new Task.");
        if (taskCompletedCallBack != null)
            taskCompletedCallBack("I have completed Task.");
    }

	public void Test()
    {
        TaskCompletedCallBack callback = callBackFunc;
        StartNewTask(callback);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值