在c#中使用一个类分两个阶段,首先定义这个类,告诉编辑器这个类由什么字段和方法组成,然后使用这个类实例化对象。在我们使用委托的时候,也需要经过这两个阶段,首先定义委托,告诉编辑器我们这个委托可以指向那些类型的方法,然后,创建该委托的实例。
定义委托的方法如下:
delegate void IntMethodInvoker(int x);
定义一个委托:IntMethodInvoker,这个委托可以指向什么类型的方法?
这个方法要带有一个 int类型的参数,并且方法的返回值是void,定义一个委托要定义方法的参数和返回值,使用关键字delegate定义。
定义案例:
delegate double TwoLongOp(long frist, long second);delegate string GetAString();private delegate string GetAString();
static void Main(){
int x = 40;
GetAString firstStringMethod = new GetAString(x.ToString);
Console.WriteLine(firstStringMethod());
}
在这里我们首先使用GetAString委托声明了一个类型叫做fristStringMethod,接下来使用new 对它进行初始化,使它引用到x中的ToString方法上,这样firstStringMethod就相当于x.ToString,我们通过firstStringMethod()执行方法就相当于x.ToString()
通过委托示例调用方法有两种方式:
fristStringMethod();firstStringMethod.Invoke();GetAString firstStringMethod = new GetAString(x.ToString);GetAString firstStringMethod = x.ToString;class MathOperations{
public static double MultiplyByTwo(double value){
return value*2;
}
public static double Square(double value){
return value*value;
}
}
delegate double DoubleOp(double x);
static void Main(){
DoubleOp[] operations={ MathOperations.MultiplyByTwo,MathOperations.Square };
for(int i =0;i<operations.Length;i++){
Console.WriteLine("Using operations "+i);
ProcessAndDisplayNumber( operations[i],2.0 );
}
}
static void ProcessAndDisplayNumber(DoubleOp action,double value){
double res = action(value);
Console.Writeline("Value :"+value+" Result:"+res);
}
Action和FuncAction委托引用了一个void返回类型的方法,T表示方法参数Action委托有哪些
Func引用了一个带有一个返回值的方法,它可以传递0或者多到16个参数类型,和一个返回类型
delegate double DoubleOp(double x); 如何用Func表示
Func<double,double>
bool swapped = true;
do{
swapped = false;
for(int i =0;i<sortArray.Length -1;i++){
if(sortArray[i]>sortArray[i+1]){
int temp= sortArray[i];
sortArray[i]=sortArray[i+1];
sortArray[i+1]=temp;
swapped = true;
}
}
}while(swapped);
class Employee{
public Employ(string name,decimal salary){
this.Name = name;
this.Salary = salary;
}
public string Name{get;private set;}
public decimal Salary{get;private set;}
public static bool CompareSalary(Employee e1,Employee e2){
return e1.salary>e2.salary;
}
}
public static void Sort<T>( List<T> sortArray,Func<T,T,bool> comparision ){
bool swapped = true;
do{
swapped = false;
for(int i=0;i<sortArray.Count-1;i++){
if(comparision(sortArray[i+1],sortArray[i])){
T temp = sortArray[i];
sortArray[i]=sortArray[i+1];
sortArray[i+1]=temp;
swapped = true;
}
}
}while(swapped);
}
static void Main(){
Employee[] employees = {
new Employee("Bunny",20000),
new Employee("Bunny",10000),
new Employee("Bunny",25000),
new Employee("Bunny",100000),
new Employee("Bunny",23000),
new Employee("Bunny",50000),
};
Sort(employees,Employee.CompareSalary);
// 输出
}
前面使用的委托都只包含一个方法的调用,但是委托也可以包含多个方法,这种委托叫做多播委托。使用多播委托就可以按照顺序调用多个方法,多播委托只能得到调用的最后一个方法的结果,一般我们把多播委托的返回类型声明为void。
Action action1 = Test1;action2+=Test2;action2-=Test1;多播委托包含一个逐个调用的委托集合,如果通过委托调用的其中一个方法抛出异常,整个迭代就会停止。
取得多播委托中所有方法的委托:
Action a1 = Method1;
a1+=Method2;
Delegate[] delegates=a1.GetInvocationList();
foreach(delegate d in delegates){
//d();
d.DynamicInvoke(null);
}
// 遍历多播委托中所有的委托,然后单独调用
Func<int,int,int> plus = delegate (int a,int b){
int temp = a+b;
return temp;
};
int res = plus(34,34);
Console.WriteLine(res);
Func<int,int,int> plus = (a,b)=>{ int temp= a+b;return temp; };
int res = plus(34,34);
Console.WriteLine(res);
Func<double,double> square = x=>x*x;
// 添加花括号,return语句和分号是完全合法的
Func<double,double> square = x=>{
return x*x;
}
int somVal = 5; Func<int,int> f = x=>x+somVal; Console.WriteLine(f(3));//8 somVal = 7; Console.WriteLine(f(3));//10
class Cat
{
string catName;
string catColor { get; set; }
public Cat(string name, string color)
{
this.catName = name;
catColor = color;
}
public void CatShout()
{
Console.WriteLine(catColor+" 的猫 "+catName+" 过来了,喵!喵!喵!\n");
//猫叫时触发事件
//猫叫时,如果CatShoutEvent中有登记事件,则执行该事件
if (CatShoutEvent != null)
CatShoutEvent();
}
public delegate void CatShoutEventHandler();
public event CatShoutEventHandler CatShoutEvent;
}
class Mouse
{
string mouseName;
string mouseColor { get; set; }
public Mouse(string name, string color)
{
this.mouseName = name;
this.mouseColor = color;
}
public void MouseRun()
{
Console.WriteLine(mouseColor + " 的老鼠 " + mouseName + " 说:\"老猫来了,快跑!\" \n我跑!!\n我使劲跑!!\n我加速使劲跑!!!\n");
}
}
Console.WriteLine("[场景说明]: 一个月明星稀的午夜,有两只老鼠在偷油吃\n");
Mouse Jerry = new Mouse("Jerry", "白色");
Mouse Jack = new Mouse("Jack", "黄色");
Console.WriteLine("[场景说明]: 一只黑猫蹑手蹑脚的走了过来\n");
Cat Tom = new Cat("Tom", "黑色");
Console.WriteLine("[场景说明]: 为了安全的偷油,登记了一个猫叫的事件\n");
Tom.CatShoutEvent += new Cat.CatShoutEventHandler(Jerry.MouseRun);
Tom.CatShoutEvent += new Cat.CatShoutEventHandler(Jack.MouseRun);
Console.WriteLine("[场景说明]: 猫叫了三声\n");
Tom.CatShout();
Console.ReadKey();
事件是一种特殊的委托,或者说是受限制的委托,是委托一种特殊应用,只能施加+=,-=操作符。二者本质上是一个东西。
event ActionHandler Tick; // 编译成创建一个私有的委托示例, 和施加在其上的add, remove方法.
event只允许用add, remove方法来操作,这导致了它不允许在类的外部被直接触发,只能在类的内部适合的时机触发。委托可以在外部被触发,但是别这么用。
使用中,委托常用来表达回调,事件表达外发的接口。
委托和事件支持静态方法和成员方法, delegate(void * pthis, f_ptr), 支持静态返方法时, pthis传null.支持成员方法时, pthis传被通知的对象.
委托对象里的三个重要字段是, pthis, f_ptr, pnext, 也就是被通知对象引用, 函数指针/地址, 委托链表的下一个委托节点.
原文:https://www.cnblogs.com/sleblogs/p/8993881.html