若是你們讀過dapper源碼,你會發現這內部有不少方法都用到了yield關鍵詞,那yield究竟是用來幹嗎的,能不能拿掉,拿掉與不拿掉有多大的差異,首先上一段dapper中精簡後的Query方法,先讓你們眼見爲實。編程
private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefinition command, Type effectiveType) { object param = command.Parameters; var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType()); var info = GetCacheInfo(identity, param, command.AddToCache); IDbCommand cmd = null; IDataReader reader = null; bool wasClosed = cnn.State == ConnectionState.Closed; try { while (reader.Read()) { object val = func(reader); if (val == null || val is T) { yield return (T)val; } else { yield return (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture); } } } }
1. 骨架代碼猜測設計模式
骨架代碼其實很簡單,方法的返回值是IEnumerable,而後return被yield開了光,讓人困惑的地方就是既然方法的返回值是IEnumerable卻在方法體內沒有看到任何實現這個接口的子類,因此第一感受就是這個yield不簡單,既然代碼能夠跑,那底層確定幫你實現了一個繼承IEnumerable接口的子類,你說對吧?app
2. msdn解釋ide
有本身的猜測還不行,還得相信權威,看msdn的解釋:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/yield函數式編程
若是你在語句中使用 yield 上下文關鍵字,則意味着它在其中出現的方法、運算符或 get 訪問器是迭代器。經過使用 yield 定義迭代器,可在實現自定義集合類型的 IEnumerator 和 IEnumerable 模式時無需其餘顯式類(保留枚舉狀態的類,有關示例,請參閱 IEnumerator)。函數
沒用過yield以前,看這句話確定是一頭霧水,只有在業務開發中踩過坑,才能體會到yield所帶來的快感。性能
3. 從IL入手this
爲了方便探究原理,我來寫一個不能再簡單的例子。編碼
public static void Main(string[] args) { var list = GetList(new int[] { 1, 2, 3, 4, 5 }); } public static IEnumerable<int> GetList(int[] nums) { foreach (var num in nums) { yield return num; } }
對,就是這麼簡單,接下來用ILSpy反編譯打開這其中的神祕面紗。spa
從截圖中看最讓人好奇的有兩點。
<1 style="box-sizing: border-box;"> 平白無故的多了一個叫作\d__1 類
好奇心驅使着我看一下這個類到底都有些什麼?因爲IL代碼太多,我作一下精簡,從下面的IL代碼中能夠發現,果真是實現了IEnumerable接口,若是你瞭解設計模式中的迭代器模式,那這裏的MoveNext,Current是否是很是熟悉?
.class nested private auto ansi sealed beforefieldinit '<GetList>d__1' extends [mscorlib]System.Object implements class [mscorlib]System.Collections.Generic.IEnumerable`1<int32>, [mscorlib]System.Collections.IEnumerable, class [mscorlib]System.Collections.Generic.IEnumerator`1<int32>, [mscorlib]System.IDisposable, [mscorlib]System.Collections.IEnumerator { .method private final hidebysig newslot virtual instance bool MoveNext () cil managed { ... } // end of method '<GetList>d__1'::MoveNext .method private final hidebysig specialname newslot virtual instance int32 'System.Collections.Generic.IEnumerator<System.Int32>.get_Current' () cil managed { ... } // end of method '<GetList>d__1'::'System.Collections.Generic.IEnumerator<System.Int32>.get_Current' .method private final hidebysig specialname newslot virtual instance object System.Collections.IEnumerator.get_Current () cil managed { ... } // end of method '<GetList>d__1'::System.Collections.IEnumerator.get_Current .method private final hidebysig newslot virtual instance class [mscorlib]System.Collections.Generic.IEnumerator`1<int32> 'System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator' () cil managed { ... } // end of method '<GetList>d__1'::'System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator' } // end of class <GetList>d__1
<2 style="box-sizing: border-box;"> GetList方法體如今會變成啥樣?
.method public hidebysig static class [mscorlib]System.Collections.Generic.IEnumerable`1<int32> GetList ( int32[] nums ) cil managed { // (no C# code) IL_0000: ldc.i4.s -2 IL_0002: newobj instance void ConsoleApp2.Program/'<GetList>d__1'::.ctor(int32) IL_0007: dup IL_0008: ldarg.0 IL_0009: stfld int32[] ConsoleApp2.Program/'<GetList>d__1'::'<>3__nums' IL_000e: ret } // end of method Program::GetList
能夠看到這地方作了一個new ConsoleApp2.Program/'d1'操做,而後進行了<>3nums=0,最後再把這個迭代類返回出來,這就解釋了爲何你的GetList能夠是IEnumerable而不報錯。
4. 打回C#代碼
你可能會說,你說了這麼多有啥用?IL代碼我也看不懂,若是能回寫成C#代碼那就了,還好回寫成C#代碼不算太難。。。
namespace ConsoleApp2 { class GetListEnumerable : IEnumerable<int>, IEnumerator<int> { private int state; private int current; private int threadID; public int[] nums; public int[] s1_nums; public int s2; public int num53; public GetListEnumerable(int state) { this.state = state; this.threadID = Environment.CurrentManagedThreadId; } public int Current => current; public IEnumerator<int> GetEnumerator() { GetListEnumerable rangeEnumerable; if (state == -2 && threadID == Environment.CurrentManagedThreadId) { state = 0; rangeEnumerable = this; } else { rangeEnumerable = new GetListEnumerable(0); } rangeEnumerable.nums = nums; return rangeEnumerable; } public bool MoveNext() { switch (state) { case 0: state = -1; s1_nums = nums; s2 = 0; num53 = s1_nums[s2]; current = num53; state = 1; return true; case 1: state = -1; s2++; if (s2 < s1_nums.Length) { num53 = s1_nums[s2]; current = num53; state = 1; return true; } s1_nums = null; return false; } return false; } object IEnumerator.Current => Current; public void Dispose() { } public void Reset() { } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }
接下來GetList就能夠是另外一種寫法了,作一個new GetListEnumerable 便可。
到目前爲止,我以爲這個yield你應該完全的懂了,不然就是個人失敗(┬_┬)...
以我本身幾年開發經驗(不想把本身說的太老(┬_┬))來看,有以下兩點好處。
1. 現階段還不清楚用什麼集合來承載這些數據
這話什麼意思?一樣的一堆集合數據,你能夠用List承載,你也能夠用SortList,HashSet甚至還能夠用Dictionary承載,對吧,你當時定義方法的時候返回值那裏是必定要先定義好接收集合,但這個接收集合真的合適嗎?你當時也是不知道的。若是你還不明白,我舉個例子:
public static class Program { public static void Main(string[] args) { //哈哈,我最後想要HashSet。。。由於我要作高效的集合去重 var hashSet1 = new HashSet<int>(GetList(new int[] { 1, 2, 3, 4, 5 })); var hashSet2 = new HashSet<int>(GetList2(new int[] { 1, 2, 3, 4, 5 })); } //編碼階段就預先決定了用List<int>承載 public static List<int> GetList(int[] nums) { return nums.Where(num => num % 2 == 0).ToList(); } //編碼階段還沒想好用什麼集合承載,有多是HashSet,SortList,鬼知道呢? public static IEnumerable<int> GetList2(int[] nums) { foreach (var num in nums) { if (num % 2 == 0) yield return num; } } }
先看代碼中的註釋,從上面例子中能夠看到我真正想要的是HashSet,而此時hashSet2 比 hashSet1 少了一箇中轉過程,無形中這就大大提升了代碼性能,對不對?
2. 可讓我無限制的疊加篩選塑形條件
這個又是什麼意思呢?有時候方法調用棧是特別深的,你沒法對一個集合在最底層進行總體一次性篩選,而是在每一個方法中實行追加式篩選塑性,請看以下示例代碼。
public static class Program { public static void Main(string[] args) { var nums = M1(true).ToList(); } public static IEnumerable<int> M1(bool desc) { return desc ? M2(2).OrderByDescending(m => m) : M2(2).OrderBy(m => m); } public static IEnumerable<int> M2(int mod) { return M3(0, 10).Where(m => m % mod == 0); } public static IEnumerable<int> M3(int start, int end) { var nums = new int[] { 1, 2, 3, 4, 5 }; return nums.Where(i => i > start && i < end); } }
上面的M1,M2,M3方法就是實現了這麼一種操做,最後使用ToList一次性輸出,因爲沒有中間商,因此靈活性和性能可想而知。
函數式編程將會是之後的主流方向,C#中幾乎全部的新特性都是爲了給函數式編程提供便利性,而這個yield就是C#函數式編程中的一個基柱,你還能夠補看Enumerable中的各類擴展方法增長一下個人說法可信度。
static IEnumerable<TSource> TakeWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate) { foreach (TSource element in source) { if (!predicate(element)) break; yield return element; } } static IEnumerable<TSource> WhereIterator<TSource>(IEnumerable<TSource> source, Func<TSource, int, bool> predicate) { int index = -1; foreach (TSource element in source) { checked { index++; } if (predicate(element, index)) yield return element; } }
好了,本篇就說到這裏,但願對你有幫助。