using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyDelegate
{
class Program
{
privatedelegatestringGetAString();//定义了一个委托GetAStringstaticvoid Main(string[] args)
{
int x = 40;
//使用委托创建实例
GetAString a = new GetAString(x.ToString);//a指向了x中的tostring方法string s1 = a();//通过委托实例去调用x中的tostring方法(常用)
GetAString b = x.ToString;//常用string s2 = b.Invoke();//通过invoke方法去调用x的tostring方法
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyDelegate
{
class Program
{
delegate void PrintMes();
staticvoid Print(PrintMes print){
print();//间接调用委托指向的方法
}
staticvoid Test1(){
Console.WriteLine("Test1");
}
staticvoid Test2(){
Console.WriteLine("Test2");
}
staticvoid Main(string[] args)
{
PrintMes pm = Test1;
Print(pm);//委托作为参数传递
pm = Test2;//委托的实例就像变量一样可以更改所指向的引用
Print(pm);
Console.ReadKey();
}
}
}
系统内置的委托类型Action:
Action是系统内置的一个委托类型,它可以指向一个没有返回值,有参数的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceMyDelegate
{classProgram
{staticvoid Test1(){
Console.WriteLine("Test1");
}
staticvoid Test2(int a){
Console.WriteLine("Test2"+a);
}
staticvoid Main(string[] args)
{
Action a = Test1;//action是系统定义的无返回值的委托
Action<int> b = Test2;//通过泛型可以指向一个没有返回值有参数的方法,参数个数为泛型的个数,用','隔开,且类型顺序与方法参数类型必须一致
Console.ReadKey();
}
}
}
系统内置的委托类型Func:
Func是系统内置的一个委托类型,它可以指向一个有返回值,有参数的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceMyDelegate
{classProgram
{staticstring Test1(){
return"Test1";
}
staticint GetRes(string a){
Console.WriteLine(a);
return100;
}
staticvoid Main(string[] args)
{
Func<string> a = Test1;//Func也是系统定义的委托,使用泛型来指定方法的返回值类型
Func<string, int> b = GetRes;//Func可以指定多个泛型,最后一个泛型指定返回值类型,其它的为参数类型
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceMyDelegate
{classProgram
{staticvoid Test1(){
Console.WriteLine("Test1");
}
staticvoid Test2(){
Console.WriteLine("Test2");
}
staticvoid Main(string[] args)
{
Action a = Test1;
a += Test2;//使用+号将方法串联起来
a();
a -= Test1;//将Test1方法移除
a();//如果委托为空,调用的时候就会报错,所有一般都会先判断空
Delegate[] delegates = a.GetInvocationList();//得到委托所调用的方法集合foreach(Delegate d in delegates){
d.DynamicInvoke();//单独调用方法,如果方法有参数,直接写在DynamicInvoke()括号里
}
}
}
}
匿名方法:
匿名方法就是没有名字的方法,所以调用的时候只能赋值给委托去调用了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyDelegate
{
class Program
{
staticvoid Main(string[] args)
{
//匿名方法
Func<int,int,int> a = delegate(int arg1,int arg2)
{
return agr1+arg2;
};
}
}
}
Lambda表达式:
lambda表达式是用来代替匿名方法的,所以一个lambda表达式也是定义了一个方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceMyDelegate
{classProgram
{staticvoid Main(string[] args)
{
//lambda表达式可以不用delegate关键字,和参数变量类型
Func<int,int,int> plus = (arg1,arg2)=>
{
return arg1+arg2;
};
Func<int,int> test = a=>a+1;//lambda表达式如果参数为一个则可以去掉括号,当函数体只有一句话时可以不加上大括号和return语句,编译器会自动添加return语句
}
}
}
事件:
在委托前面加上event后便成了事件,事件的基本用法与委托基本一样,区别在于,事件不能在类外触发
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyDelegate
{
class Program
{
publicdelegatevoidTestDelegate();
publicevent TestDelegate testDelegate;//声明事件staticvoid Main(string[] args)
{
}
}
}