using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console
{
class Program
{
/*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*委托对象通常通过提供委托将封装的方法的名称或使用匿名方法构造。
*对委托进行实例化后,委托会将对其进行的方法调用传递到该方法。
*调用方传递到委托的参数将传递到该方法,并且委托会将方法的返回值(如果有)返回到调用方。
*这被称为调用委托。 实例化的委托可以按封装的方法本身进行调用。 例如:
*
*1.使用委托类型,貌似委托类型定义不能在方法中定义
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
*/
//委托的定义(可以看成是函数的抽象)
public delegate void Del(string message);
// Create a method for a delegate.
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
}
public static void MethodWithCallback(int param1, int param2, Del callback)
{ /*此时调用的委托当作一个参数方法传入函数里,类似匿名方法的传递,类似php的回调函数机制。
*此种方式可以对函数追加额外的逻辑
*此处是否会涉及到函数作用域的问题
*/
callback("The number is: " + (param1 + param2).ToString());
}
static void Main(string[] args)
{
// Instantiate the delegate.
Del handler = DelegateMethod;
//1. Call the delegate.实列化委托的方式进行调用,相当于函数赋值给一个变量的引用进行委托
handler("Hello World");
//2.作为回掉函数进行调用
MethodWithCallback(1, 2, handler);
//3.多方法委托,以及在类中进行委托方式
/*
* 此时,allMethodsDelegate 的调用列表中包含三个方法,分别为 Method1、Method2 和 DelegateMethod。
* 原有的三个委托(d1、d2 和 d3)保持不变。 调用 allMethodsDelegate 时,将按顺序调用所有三个方法。
* 如果委托使用引用参数,引用将按相反的顺序传递到所有这三个方法,并且一种方法进行的任何更改都将在另一种方法上见到。
* 当方法引发未在方法内捕获到的异常时,该异常将传递到委托的调用方,并且不会调用调用列表中的后续方法。
* 如果委托具有返回值和/或输出参数,它将返回上次调用方法的返回值和参数。 若要删除调用列表中的方法,请使用减法运算符或减法赋值运算符(“+”或“+=”)。 例如:
*/
MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = DelegateMethod;
//Both types of assignment are valid.
Del allMethodsDelegate = d1 + d2;
allMethodsDelegate += d3;
//remove Method1
allMethodsDelegate -= d1;
// copy AllMethodsDelegate while removing d2
Del oneMethodDelegate = allMethodsDelegate - d2;
//4.查看委托调用的方法数量:由于委托类型派生自 System.Delegate,因此可以在委托上调用该类定义的方法和属性。
//例如,若要查询委托调用列表中方法的数量,你可以编写
/* 5.调用列表中具有多个方法的委托派生自 MulticastDelegate,该类属于 System.Delegate 的子类。
* 由于这两个类都支持 GetInvocationList,因此在其他情况下,上述代码也将产生作用。
* 多播委托广泛用于事件处理中。 事件源对象将事件通知发送到已注册接收该事件的接收方对象。
* 若要注册一个事件,接收方需要创建用于处理该事件的方法,然后为该方法创建委托并将委托传递到事件源。
* 事件发生时,源调用委托。 然后,委托将对接收方调用事件处理方法,从而提供事件数据。 给定事件的委托类型由事件源确定。
* 6.在编译时比较分配的两个不同类型的委托将导致编译错误。 如果委托实例是静态的 System.Delegate 类型,则允许比较,但在运行时将返回 false。
*/
delegate void Delegate1();
delegate void Delegate2();
static void method(Delegate1 d, Delegate2 e, System.Delegate f)
{
// Compile-time error.
//Console.WriteLine(d == e);
// OK at compile-time. False if the run-time type of f
// is not the same as that of d.
System.Console.WriteLine(d == f);
}
}
/**
* 多委托使用+号进行拼接
*
*
*/
public class MethodClass {
public void Method1(string message) { }
public void Method2(string message) { }
}
}
转载于:https://my.oschina.net/u/998304/blog/376542