委托: 这个比较简单,从delegate关键字到匿名委托方法,再到Action和Func,Action是无返回值的委托,Func是有返回值的委托,现在基本上都是基于Action和Func来写委托的,有些和业务逻辑连
委托:
这个比较简单,从delegate关键字到匿名委托方法,再到Action和Func,Action是无返回值的委托,Func是有返回值的委托,现在基本上都是基于Action和Func来写委托的,有些和业务逻辑连接起来写匿名委托方法。
首先建立一个student类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public class Student
{
public int ID { get; set; }
public string name { get; set; }
public int age { get; set; }
public string sex { get; set; }
public string grade { get; set; }
}
}
2.我们初始化数据后,分别进行查询操作
进行3次查询后,然后分别输出对应的名字,这样是我们平时的做法,一点错误都没有。可以看到都是where的功劳,现在我们可以使用泛型委托自己自定义一个和where一样的功能。
只要遇到代码有重复的逻辑,就可以使用委托
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp4
{
static class Program
{
static void Main(string[] args)
{
List<Student> student = new List<Student>();
student.Add(new Student() { age = 20, grade = "一班", ID = 1, name = "张三", sex = "男" });
student.Add(new Student() { age = 21, grade = "一班", ID = 2, name = "李四", sex = "女" });
student.Add(new Student() { age = 22, grade = "二班", ID = 3, name = "王五", sex = "女" });
student.Add(new Student() { age = 23, grade = "二班", ID = 4, name = "老六", sex = "男" });
var stu = student.Where(s => s.grade == "一班");
foreach (var item in stu)
{
Console.WriteLine(item.name);
}
Console.WriteLine("==============分界线==============");
var stu1 = student.Where(s => s.sex == "男");
foreach (var item in stu1)
{
Console.WriteLine(item.name);
}
Console.WriteLine("==============分界线==============");
var stu2 = student.Where(s => s.sex == "男" && s.age > 20);
foreach (var item in stu2)
{
Console.WriteLine(item.name);
}
}
}
}
但是我们建立一个WhereNew类,表示泛型委托类 特别要注意,Invoke是执行方法,传入参数,返回bool值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public static class WhereNew
{
public static IEnumerable<T> NewWhere<T>(this IEnumerable<T> items, Func<T, bool> func)
{
List<T> ls = new List<T>();
foreach (var item in items)
{
if (func.Invoke(item)) //执行方法,传入参数,返回bool值
{
ls.Add(item);
}
}
return ls;
}
}
}
在使用
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp4
{
static class Program
{
static void Main(string[] args)
{
List<Student> student = new List<Student>();
student.Add(new Student() { age = 20, grade = "一班", ID = 1, name = "张三", sex = "男" });
student.Add(new Student() { age = 21, grade = "一班", ID = 2, name = "李四", sex = "女" });
student.Add(new Student() { age = 22, grade = "二班", ID = 3, name = "王五", sex = "女" });
student.Add(new Student() { age = 23, grade = "二班", ID = 4, name = "老六", sex = "男" });
//var stu = student.Where(s => s.grade == "一班");
IEnumerable<Student> stu = student.NewWhere(s => s.grade == "一班");
foreach (var item in stu)
{
Console.WriteLine(item.name);
}
Console.WriteLine("==============分界线==============");
//var stu1 = student.Where(s => s.sex == "男");
IEnumerable<Student> stu1 = student.NewWhere(s => s.sex == "男");
foreach (var item in stu1)
{
Console.WriteLine(item.name);
}
Console.WriteLine("==============分界线==============");
//var stu2 = student.Where(s => s.sex == "男" && s.age > 20);
IEnumerable<Student> stu2 = student.NewWhere(s => s.sex == "男" && s.age > 20);
foreach (var item in stu2)
{
Console.WriteLine(item.name);
}
}
}
}
从中我们可以看到where和自定义的NewWhere,功能是一样的,也就是说where也是用了泛型委托。泛型就是T,委托就是func。所以,我们可以传递一个类型的数据进去,然后再传递一个方法进去,自动就加工返回值了。