一 委托定义
1.1 官方解释
委托是一种定义方法签名的类型。当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联。您可以通过委托实例调用方法。
1.2 个人理解
委托就是执行方法(函数)的一个类。
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。
二 委托的声明
(1). delegate
delegate我们常用到的一种声明
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
例:public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型。
(2). Action
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
(3). Func
Func是有返回值的泛型委托
Func<int> 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
(4) .predicate
predicate 是返回bool型的泛型委托
predicate<int> 表示传入参数为int 返回bool的委托
Predicate有且只有一个参数,返回值固定为bool
例:public delegate bool Predicate<T> (T)
三 委托的使用
3.1 Labmda表达式
TestDelegate d2= (string name) => { Console.WriteLine("你好,{0}!", name); };
d2("Terry");
3.2匿名方法
delegate void TestDelegate(string myName);
TestDelegate d2 = delegate(string name)
{
Console.WriteLine("Hello,{0}!", name);
};
d2(“Test”);
3.3 函数申明
private void DelegateMethod(string name)
{
Console.WriteLine("Hello,{0}!", name);
}
TestDelegate d2 = new TestDelegate(DelegateMethod);
d2(“Test”);
四、使用委托有哪些特点
委托类似于 C++ 函数指针,但它们是类型安全的。
委托允许将方法作为参数进行传递。
委托可用于定义回调方法。
委托可以链接在一起;例如,可以对一个事件调用多个方法。
方法不必与委托签名完全匹配。
委托一般都使用在 Observer模式(观察者模式)。
Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。
Observer模式主要包括如下两类对象:
被监视对象:往往包含着其他对象所感兴趣的内容。
监视者:当对象中的某件事发生的时候,会告知建设者,而建设者则会采取相应的行动。
例如:当你程序处理大批量数据时,需要在程序界面显示进度条进行友好提示,这时你通过委托来实现相当方便。
namespace Test
{
public delegate void DelegateMethod(int position, int maxValue);
public class TestDelegate
{
public DelegateMethod OnDelegate;
/// <summary>
/// 通知所以委托 DelegateMethod
/// </summary>
public void DoDelegateMethod()
{
int maxValue = 100;
for (int i = 0; i < maxValue; i++)
{
if (this.OnDelegate != null)
{
this.OnDelegate(i, maxValue);
}
}
}
}
}
TestDelegate test = new TestDelegate();
this.textBox1.Text = "";
this.progressBar1.Value = 0;
test.OnDelegate = new DelegateMethod(delegate (int i, int maxValue)
{
this.textBox1.Text += i.ToString() + Environment.NewLine;
this.progressBar1.Maximum = maxValue;
this.progressBar1.Value++;
});
test.DoDelegateMethod();
五 使用
(1) Delegate
public class Price
{
public double Ordinary(double price)
{
double price1 = 0.95 * price;
Console.WriteLine("Ordinary Price : " + price1);
return price1;
}
public double Favourable(double price)
{
double price1 = 0.85 * price;
Console.WriteLine("Favourable Price : " + price1);
return price1;
}
static void Main(string[] args)
{
Price price = new Price();
//绑定Ordinary方法
MyDelegate myDelegate = new MyDelegate(price.Ordinary); //第一个先初始化
//绑定Favourable方法
myDelegate += new MyDelegate(price.Favourable); //其余用+= 增加 用 -=清除 (会依次执行先加入的先执行最后的输出结果是最后一个方法)
//调用委托
Console.WriteLine("Current Price : " + myDelegate(100));
}
}
}
输出
//清除法1
Console.WriteLine("Current Price : " + myDelegate(100));
while (myDelegate != null)
{
myDelegate -= myDelegate;
}
//清除法2
if (myDelegate != null)
{
System.Delegate[] dels = myDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
myDelegate -= dels[i] as MyDelegate;
}
}
(2) Action的使用
Test<string>(Action, "Hello World!");
Test<int>(Action, 1000);
Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表达式定义委托
Console.ReadKey(); }
public static void Test<T>(Action<T> action, T p)
{
action(p);
}
private static void Action(string s)
{
Console.WriteLine(s);
}
private static void Action(int s)
{
Console.WriteLine(s);
}
可以使用 Action<T1, T2, T3, T4> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有四个均通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void)通常,这种方法用于执行某个操作。
(3) Func的使用
static void Main(string[] args)
{
Console.WriteLine(Test<int,int>(Fun,100,200));
Console.ReadKey();
}
public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
{
return func(a, b);
}
private static int Fun(int a, int b)
{
return a + b;
}
(4) predicate的使用
泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
static void Main(string[] args)
{
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
Point first = Array.Find(points, ProductGT10);
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
Console.ReadKey();
}
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
使用带有 Array.Find 方法的 Predicate 委托搜索 Point 结构的数组。如果 X 和 Y 字段的乘积大于 100,000,此委托表示的方法 ProductGT10 将返回 true。Find 方法为数组的每个元素调用此委托,在符合测试条件的第一个点处停止。