Recently learned the next python, found that there is also the use of yield, originally on C # yield is not very understanding, but through learning Python, C # yield understanding deeper!!
Not much to say, the level of pupils can not hurt the ability to express ....
Directly on the code:
Using system;using system.collections;using system.collections.generic;using system.linq;using System.Text; Namespace consoleapplication2{class Program {static void Main (string[] args) {foreach (V Ar obj in YHSJ (8)) {Printrow (obj); } console.readkey (); }///<summary>//Get an iterator to the Yang Hui triangle///</summary>//<param name= "n" ></param >//<returns></returns> public static ienumerable<ienumerable<int>> yhsj (int n) {//First line list<int> row = new List<int> {1}; yield return row; Second row = new List<int> {1, 1}; yield return row; The third row and its subsequent while (n > 0) {list<int> NewRow = new List<int> {1, 1}; int index = row. Count; for (int i = 1; I &lT Index i++) {NewRow. Insert (1, row[i-1] + row[i]); } yield return newrow; row = NewRow; n--; }}///<summary>///print a row of data///</summary>//<param name= "Row" >< ;/param> public static void Printrow (Ienumerable<int> row) {foreach (int i in row) {Console.Write (i + "\ t"); } Console.WriteLine (); } }}
Output Yang Hui triangle with yield keyword