19. C # introduce the extension methods (10.3-10.5) in IEnumerable and IEnumerable <T> one by one ),
Today, there are not many words, only code, and there are too many extension methods. You cannot complete them one by one. If you read more books, you will use them.
1 // Enumerable. range returns the start to end Range. It is an Enumrable <int> type 2 // The Range method does not really construct a list containing the appropriate numbers, it only generates those numbers at the right time, "just in time" 3 var c0 = Enumerable. range (1, 10); 4 foreach (var e in c0) 5 {6 Console. writeLine (e); // print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 7} 8 9 // Reverse (), Reverse list 10 var c1 = c0.Reverse (); 11 foreach (var e in c1) 12 {13 Console. writeLine (e); // print 10, 9, 8, 7, 6, 5, 4, 3, 2,114} 15 16 // where (), filter 17 var c2 = c1.Where (x => x % 2 = 0); // return elements that meet the predicates, the returned type is Enumrable <int> 18 foreach (var e in c2) 19 {20 Console. writeLine (e); // 221,} 22 23 Console. writeLine ("-----------------------------------------"); 24 25 // use a chained operation because each operation returns Enumrable <int> 26 c0.Reverse (). where (x => x % 2 = 0 ). toList (). forEach (x => Console. writeLine (x); 27 28 // select (), for projection, returns the custom type Object List 29 string [] arrs = {"James", "John ", "Michelle", "Amy", "Kim"}; 30 31 // a list of anonymous types is returned, including the First and Last attributes 32 var objs = arrs. select (x => new {First = x. first (), Last = x. last ()}); 33 foreach (var e in objs) 34 {35 Console. writeLine (e. first); // J, J, M, A, K36 Console. writeLine (e. last); // s, n, e, y, m37} 38 39 // select an element starting with "J, move Y from the first place to the unspecified 40 var yObjs = arrs. toList (). findAll (x => x. startsWith ("J ")). select (x => 41 {42 var y = x. remove (0, 1); 43 return y + "J"; 44}); 45 foreach (var e in yObjs) 46 {47 Console. writeLine (e); // amesJ, ohnJ48} 49 50 // use OrderBy for sorting, return IOrderedEnumerable <string> 51 var orderArrs = arrs. orderBy (x => x. first (); 52 foreach (var e in orderArrs) 53 {54 Console. writeLine (e); // Amy, James, John, Kim, Michelin 55} 56 57 // ThenBy (), first sort OrderBy () by the first letter, 58 var thenArrs = arrs. orderBy (x => x. first ()). thenBy (x => x. last (); 59 foreach (var e in thenArrs) 60 {61 Console. writeLine (e); // Amy, John, James, Kim, Michelin 62}
Please make an axe.