什么是委托 Delegate
- 委托(delegate)是函数指针的“升级版”
- 一切皆地址
1、变量(数据)是以某个地址为起点的一段内存中所存储的值
2、函数(算法)是以某个地址为起点的一段内存中所存储的一组机器语言指令 - 直接调用与间接调用
1、直接调用:通过函数名来调用函数,CPU 通过函数名直接获得函数所在地址并开始执行 -> 返回
2、间接调用:通过函数指针来调用函数,CPU 通过读取函数指针存储的值获得函数所在地址并开始执行 -> 返回
普通的方法调用方式
namespace @delegate
{
class Program
{
static void Main(string[] args)
{
int x = 100;
int y = 200;
int z = Add(x, y);
Console.WriteLine("Add result:{0}", z);
Console.ReadKey();
int Add(int a, int b)
{
int result = a + b;
return result;
}
}
}
}
委托的调用方式
Action 和 Func 是 C# 内置的委托实例,它们都有很多重载以方便使用。
namespace c_sharp_calculate
{
class Program
{
static void Main(string[] args)
{
Calcuator calcuator = new Calcuator();
Action action = new Action(calcuator.Report);//Action委托指向了calculator方法
calcuator.Report();//直接调用
action.Invoke();//间接调用
action();
//Func是集合的委托,有多种重载方式可以使用
Func<int, int, int> func1 = new Func<int, int, int>(calcuator.Add);//这个重载方式适合指向calculator.Add方法
Func<int, int, int> func2 = new Func<int, int, int>(calcuator.Sub);
int x = 200;
int y = 100;
int z = 0;
int s = 0;
z = func1(x, y);
//z = func1.Invoke(x, y);
Console.WriteLine(z);
s = func2(x, y);
Console.WriteLine(s);
Console.ReadKey();
}
}
class Calcuator
{
public void Report()
{
Console.WriteLine("l have 3 methods.");
}
public int Add(int a, int b)
{
int result = a + b;
return result;
}
public int Sub(int a,int b)
{
int result = a - b;
return result;
}
}
}
委托是一个类
namespace action_class
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(Action);
Console.WriteLine(t.IsClass);
Console.ReadKey();
}
}
}
委托的声明方法
namespace DelegateExample
{
public delegate double Calc(double x, double y);//声明一个自定义委托,委托应设置在名称空间中
class Program
{
static void Main(string[] args)
{
Calculator calculate = new Calculator();//实例化
Calc cal1 = new Calc(calculate.Add);//创建委托,指向目标的方法,方法和委托的参数类型和返回值类型必须一样
Console.WriteLine(cal1(200, 100));//使用委托进行间接调用
Console.ReadKey();
}
}
class Calculator
{
public double Add(double x, double y)
{
return x + y;
}
public double Sub(double x, double y)
{
return x - y;
}
}
}
委托的一般使用
模版方法
利用模板方法,提高代码复用性。
回调方法
回调方法是通过委托类型参数传入主调方法的被调用方法,主调方法根据自己的逻辑决定是否调用这个方法。
namespace WrapFactory
{
//模版方法的好处:代码重复使用
class Program
{
static void Main(string[] args)
{
ProductFactory productfactory = new ProductFactory();
WrapFactory wrapfactory = new WrapFactory();
//声明一个委托实例,封装MakePizza的方法
Func<Product> func1 = new Func<Product>(productfactory.MakePizza);
Func<Product> func2 = new Func<Product>(productfactory.MakeCoke);
//声明log的委托
Logger logger = new Logger();
Action<Product> log = new Action<Product>(logger.Log);
Box box1 = wrapfactory.WrapProduct(func1,log);//包装产品1
Box box2 = wrapfactory.WrapProduct(func2,log);//包装产品2
Console.WriteLine(box1.Product.Name);
Console.WriteLine(box2.Product.Name);
Console.ReadKey();
}
}
class Logger//回调方法
{
public void Log(Product product)
{
Console.WriteLine("Product '{0}' created at {1}, Price is {2}.", product.Name, DateTime.UtcNow, product.Price);
}
}
class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
class Box
{
public Product Product { get; set; }
}
class WrapFactory
{
public Box WrapProduct(Func<Product> getProduct,Action<Product> logCallback)//这是一个模版方法,使用一个委托的product,返回包装好的box
{
Box box = new Box();
Product product = getProduct.Invoke();//获取产品
if (product.Price>=50)
{
logCallback(product);
}
box.Product = product;
return box;
}
}
class ProductFactory
{
public Product MakePizza()
{
Product product = new Product();
product.Name = "Pizza";
product.Price = 30;
return product;
}
public Product MakeCoke()
{
Product product = new Product();
product.Name = "Coke";
product.Price = 200;
return product;
}
}
}