无返回类型的内置委托—Action
action委托不具备传入参数且无返回值
如何使用Action委托,如果我们定义需要一个委托来装填一个方法,而且这个委托是无传入参数而且无返回值的时候可以我们回这样子写代码
public delegate void ShowValue(); public class Name { private string instanceName; public Name(string Name) { this.instanceName = Name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindows() { Console.WriteLine(this.instanceName + "test"); } }
在这里我们定义了一个屋传入参数且无返回值的委托,并且定义了一个类Name,并且添加了一个构造函数,和两个不同的方法实现
Name testName = new Name("billy"); ShowValue showMethord = delegate() { testName.DisplayToWindows(); }; showMethord();
我们会这样子调用,先实例化一个Name,并且实例化一个ShowValue的委托,并给委托ShowValue注册testName的方法;
如果这时候我们使用内置的Action委托,则无需要自己在自定义一个委托,
public delegate void ShowMethord();
则可以省略掉
Action showMethord = testName.DisplayToConsole;
showMethord();
使用Action就可以让代码变的简单了
Aciont<T>的使用要了解
List<Person> personList = GetPersonList(); personList.ForEach(new Action<Person>(delegate(Person p) { Console.WriteLine(p.id); }));
简化其写法
personList.ForEach(delegate(Person p){ console.writeline(p.id); })
编译器会自动帮我们识别该方法的委托是否有返回值,如果无返回值则编译器会自动将我们传入的匿名函数换成是Action<T>形式的委托
有返回类型的内置委托—Func
委托Func的定义方法
public delegate TResult Func<in T, out TResult>(T arg)
(1)in T :此委托封装的方法的参数类型。
(2)out TResult :此委托封装的方法的返回值类型。
不使用Func定义委托的时候我们的代码如下
public delegate string mydelegate(string test); class Program { static void Main(string[] args) { mydelegate mydelegate1 = new mydelegate(mytest); Console.WriteLine(mydelegate1("test")); } public static string mytest(string test) { return test + "haha"; } }
使用Func来定义委托
Func<string, string> test = delegate(string mytest1) { return mytest1 + "test"; }; Console.WriteLine(test("haha"));
用Func来定义委托是不是更简单方便了呢
使用Func的实例
List中的select
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
可以看到select的方法是IEnumerable地下的一个扩展方法,其传入参数定义了一个泛型委托Func,其传入参数为Tsource,其返回值是TResult
①标准定义版:
List<People> peopleList = new List<People>{ new People{Id=1,Name="a",Age=12}, new People{Id=2,Name="b",Age=13}, new People{Id=3,Name="c",Age=14 } }; IEnumerable<LitePerson> litePerson = peopleList.Where<People>(new Func<People, bool>(delegate(People p1) { return p1.Id == 2; })) .Select<People, LitePerson>(new Func<People, LitePerson>(delegate(People p) { return new LitePerson { Name = p.Name }; })); foreach (LitePerson p in litePerson) { Console.WriteLine(p.Name); } Console.ReadLine();
②简化版:借助编译器提供的自动识别(使用匿名函数)
IEnumerable<LitePerson> liteperson = peopleList.Where(delegate(People p) { return p.Id == 2; }).Select(delegate(People p) { return new LitePerson { Name = p.Name }; }); foreach (LitePerson p in liteperson) { Console.WriteLine(p.Name); } Console.ReadLine();
③最简版:使用匿名类
var liteperson = peopleList.Where(delegate(People p) { return p.Id == 2; }).Select(delegate(People p) { return new { Name = p.Name }; }); foreach var in liteperson) { Console.WriteLine(p.Name); } Console.ReadLine();
Predicate
也是一种特殊的委托只不过是返回值是为Bool类型的Predicate的泛型委托
现在我们来看一下list<T>中的FindAll方法中的参数定义了一个
public List<T> FindAll(Predicate<T> match);
代码如下
List<People> agedList = peopleList.FindAll(new Predicate<People>(delegate(People p) { return p.Id > 1; })); foreach (People p in agedList) { Console.WriteLine(p.Name); } Console.ReadLine();
可以看出Predicate的泛型委托是返回bool值类型的特殊泛型委托
Comparison
也是一种特殊的委托只不过是返回值是为Int类型的Predicate的泛型委托
定义的方法
public delegate int Comparison<in T>(T x, T y)
同样我们看一下list<T>中的的sort方法
public void Sort(Comparison<T> comparison);