C# --- 委托机制 delegate 和 回调 callback
- 什么是委托机制
- 委托机制的优点
- C# 中的Action 和 Func
- 委托机制的主要用处 --- 回调 Callback
什么是委托机制
- 委托机制相当于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;
委托机制的主要用处 — 回调 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);
    }
}



















