C# Linq,扩展方法,迭代器,Lambda
1.匿名类:
假设存在Student实体表。
// 第一种:
Student student = new Student();
student.Id = 1;
student.Name = "大水治禹";
student.Age = 25;
student.ClassId = 2;
// 第二种:
Student student = new Student()
{
Id = 1,
Name = "大水治禹",
Age = 25,
ClassId = 2
};
// 第三种:匿名类
object model = new
{
Id = 2,
Name = "undefined",
Age = 25,
ClassId = 2
};
// 这样写是有问题的
Console.WriteLine(model.Id);//object 编译器不允许
// 第四种:匿名类
var varModel = new
{
Id = 2,
Name = "undefined",
Age = 25,
ClassId = 2
};
// 只能 Get
Console.WriteLine(varModel.Id);
// 不能 Set
varModel.Id = 123;//只能get 没有set,通过反射可以找到
// 第五种
dynamic dModel = new//4.0
{
Id = 2,
Name = "undefined",
Age = 25,
ClassId = 2
};
// 只能 Get
Console.WriteLine(dModel .Id);
// 不能 Set
dModel .Id = 123;//只能get 没有set
2.扩展方法:
扩展方法:静态类里面的静态方法,第一个参数类型前面加上this。
用途:可以不修改类,增加方法;也就是方便一点。
缺陷:优先调用类型的实例方法(有隐患)。
扩展基类型,导致任何子类都有这个方法,而且还可能被覆盖啥的。
// 这是一个简单的数据查询的扩展方法
public static List<T> ElevenWhere<T>(this List<T> source, Func<T, bool> func)
{
var list = new List<T>();
foreach (var item in source)
{
// 将数据传入委托,筛选
if (func.Invoke(item))
{
list.Add(item);
}
}
return list;
}
3.迭代器:
// 这是一个简单的数据查询的扩展方法(添加了迭代器)
public static IEnumerable<T> ElevenWhere<T>(this IEnumerable<T> source, Func<T, bool> func)
{
if (source == null)
{
throw new Exception("source is null");
}
if (func == null)
{
throw new Exception("func is null");
}
foreach (var item in source)
{
if (func.Invoke(item))
{
// 添加了迭代器的使用
yield return item;
}
}
}
1.yield return 表示在迭代中下一个迭代时返回的数据,除此之外还有yield break, 其表示跳出迭代。
2.在.NET中,迭代器模式被IEnumerator和IEnumerable及其对应的泛型接口所封装。如果一个类实现了IEnumerable接 口,那么就能够被迭代,yield 需要和IEnumerable搭配之用。
3.调用GetEnumerator方法将返回IEnumerator接口的实现,它就是迭代器本身。迭代器类似数据库中的游标,他是 数据序列中的一个位置记录。
4.迭代器只能向前移动,同一数据序列中可以有多个迭代器同时对数据进行操作。
5.迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式。
6.简单来说,迭代器模式使得你能够获取到序列中的所有元素 而不用关心是其类型是array,list,linked list或者是其他什么序列结构。
7.等到该函数下一次被调用时,会从上一次中断的地方开始执行,一直遇到下一个yield, 程序返回值, 并在此保存当前状态; 如此反复,直到函数正常执行完成。
4.Lambda:
public delegate void NoReturnNoPara();
public delegate void NoReturnWithPara(int x, int y);//1 声明委托
public delegate int WithReturnNoPara();
public delegate string WithReturnWithPara(out int x, ref int y);
// 执行的方法体
private void DoNothing()
{
Console.WriteLine("This is DoNothing");
}
// 拉姆达表达式演化:
{
// 1.0
NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);
}
{
// 2.0 匿名方法 (相当于把执行的方法体放进去,然后将 private void DoNothing 替换为 delegate )
NoReturnNoPara method = new NoReturnNoPara(delegate ()
{
Console.WriteLine("This is DoNothing1");
});
}
{
// 3.0 lambda:左边是参数列表 goes to 右边是方法体 本质就是一个方法
NoReturnNoPara method = new NoReturnNoPara(() =>// => 表示 goes to
{
Console.WriteLine("This is DoNothing2");
});
}
{
NoReturnWithPara method = new NoReturnWithPara((int x, int y) => {
Console.WriteLine("This is DoNothing3");
});
// 参数类型可以省略 自动推算的
NoReturnWithPara method = new NoReturnWithPara((x, y) => {
Console.WriteLine("This is DoNothing4");
});
// 方法体只有一行,可以去掉大括号和分号
NoReturnWithPara method = new NoReturnWithPara((x, y) =>
Console.WriteLine("This is DoNothing5")
);
// ()也可以省略
NoReturnWithPara method = (x, y) => Console.WriteLine("This is DoNothing6");
}
Lambda表达式实际上是一种匿名函数,在Lambda表达式中可以包含语句以及运算等操作。并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。使用Lambda表达式可大大减少代码量,使得代码更加的优美、简洁,更有可观性。
5.Linq:
Linq查询基本操作(查询关键字)
- from 子句
- where 子句
- select子句
- group 子句
- into 子句
- orderby 子句
- join 子句
- let 子句