IEnumerable、IEnumerator
这两个接口位于System.Collections.Generic命名空间下。
IEnumrable
源码:https://referencesource.microsoft.com/#mscorlib/system/collections/ienumerable.cs,9be451ac13d86a97
[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
[System.Runtime.InteropServices.ComVisible(true)]
public interface IEnumerable
{
// Interfaces are not serializable
// Returns an IEnumerator for this enumerable Object. The enumerator provides
// a simple way to access all the contents of a collection.
[Pure]
[DispId(-4)]
IEnumerator GetEnumerator();
}
实际上就一个方法:IEnumerator GetEnumerator();
IEnumerator
源码:
public interface IEnumerator
{
// Interfaces are not serializable
// Advances the enumerator to the next element of the enumeration and
// returns a boolean indicating whether an element is available. Upon
// creation, an enumerator is conceptually positioned before the first
// element of the enumeration, and the first call to MoveNext
// brings the first element of the enumeration into view.
//
bool MoveNext();
// Returns the current element of the enumeration. The returned value is
// undefined before the first call to MoveNext and following a
// call to MoveNext that returned false. Multiple calls to
// GetCurrent with no intervening calls to MoveNext
// will return the same object.
//
Object Current {
get;
}
// Resets the enumerator to the beginning of the enumeration, starting over.
// The preferred behavior for Reset is to return the exact same enumeration.
// This means if you modify the underlying collection then call Reset, your
// IEnumerator will be invalid, just as it would have been if you had called
// MoveNext or Current.
//
void Reset();
}
很明显,如果一个类实现了这两个方法,就能很方便的遍历该类所包含的一些数据。为了使用更加方便,c#更是用foreach关键字来简化使用。
var
匿名类型(隐式类型),就是让编译器猜测数据类型。
var i = new int[] {1,2,3}; //编译器能猜出来`i`是个int数组。
匿名类
var i = new {name = "kun",age ="20"};
我们并没有定义一个类,但是使用的时候直接i.name
就能得到kun字符串。
lambda表达式
将函数的声明类型、return等关键字优化掉
(t)=>console.writline(t.tostring());
扩展方法
以Where方法为例,where方法是命名空间System.Linq
下public static class Enumerable
的方法,注意:Enumerable类并不是IEnumerable接口的实现。Enumerable类下提供一组用于查询实现 IEnumerable 的对象的 static方法。也就是说Enumerable类下的方法都实现了IEnumerable接口。
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
return new WhereEnumerableIterator<TSource>(source, CombinePredicates(this.predicate, predicate));
}
class WhereEnumerableIterator<TSource> : Iterator<TSource>
{
IEnumerable<TSource> source;
Func<TSource, bool> predicate;
IEnumerator<TSource> enumerator;
public WhereEnumerableIterator(IEnumerable<TSource> source, Func<TSource, bool> predicate) {
this.source = source;
this.predicate = predicate;
}
public override Iterator<TSource> Clone() {
return new WhereEnumerableIterator<TSource>(source, predicate);
}
public override void Dispose() {
if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose();
enumerator = null;
base.Dispose();
}
public override bool MoveNext() {
switch (state) {
case 1:
enumerator = source.GetEnumerator();
state = 2;
goto case 2;
case 2:
while (enumerator.MoveNext()) {
TSource item = enumerator.Current;
if (predicate(item)) { //在这里使用传入的lambda表达式筛选下一个item
current = item;
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector) {
return new WhereSelectEnumerableIterator<TSource, TResult>(source, predicate, selector);
}
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
return new WhereEnumerableIterator<TSource>(source, CombinePredicates(this.predicate, predicate));
}
}
表达式树
namespace SqlSugar
{
public class JoinQueryInfo
{
public JoinType JoinType{ get; set; }
public string TableName { get; set; }
public string ShortName { get; set; }
public int JoinIndex { get; set; }
public string JoinWhere { get; set; }
}
}