User Tools

Site Tools


notes:csharp:yield

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:yield [2015/10/28]
admin
notes:csharp:yield [2016/12/14] (current)
admin
Line 5: Line 5:
 The //yield return// statement allows you to return one element during each iteration. This makes it possible to iterate through data without reading all of it into memory in one turn. The //yield return// statement allows you to return one element during each iteration. This makes it possible to iterate through data without reading all of it into memory in one turn.
  
-**Example**: Create an enumerator returning a power of 2 in each iteration:+Example: Create an enumerator returning a power of 2 in each iteration:
 <code csharp> <code csharp>
-// The Power method has to have a return type of IEnumerable,​ IEnumerator, ​ +// The Power method has to have a return type of IEnumerable,​ IEnumerator,​ or one of the generic equivalents. ​
-// or one of the generic equivalents. ​+
 public IEnumerable Power(int number, int exponent) public IEnumerable Power(int number, int exponent)
 { {
Line 21: Line 20:
     }     }
 } }
- 
 ... ...
- 
 // Display the powers of 2: 2 4 8 16 32 64 128 256 // Display the powers of 2: 2 4 8 16 32 64 128 256
 foreach (int i in Power(2, 8))  foreach (int i in Power(2, 8)) 
  
-    Console.Write("​{0} ", i); +    Console.Write($"{i} "​); ​
  
 </​code>​ </​code>​
  
  
-**Example**: Create a generic enumerator returning a value of an enum in each iteration:+Example: Create a generic enumerator returning a value of an enum in each iteration:
 <code csharp> <code csharp>
 public enum WeekdayEnum public enum WeekdayEnum
Line 44: Line 41:
     Saturday     Saturday
 } }
- 
 ... ...
- 
 private IEnumerable<​WeekdayEnum>​ Weekday private IEnumerable<​WeekdayEnum>​ Weekday
 { {
-    // We return enum values in a different order than defined.+    // return enum values in a different order than defined
     get     get
     {     {
Line 61: Line 56:
     }     }
 } }
- 
 ... ...
- 
 foreach (WeekdayEnum d in Weekday) foreach (WeekdayEnum d in Weekday)
 { {
Line 72: Line 65:
  
  
-**Example**: Create an enumerator returning a line from a text file in each iteration: ​+Example: Create an enumerator returning a line from a text file in each iteration: ​
 <code csharp> <code csharp>
 IEnumerable<​string>​ ReadLines(string filename) IEnumerable<​string>​ ReadLines(string filename)
Line 85: Line 78:
     }     }
 } }
- 
 ... ...
- 
 foreach (string line in ReadLines("​data.txt"​)) foreach (string line in ReadLines("​data.txt"​))
 { {
Line 95: Line 86:
  
  
-**Example**: Implement an enumerator that utilizes an indexer:+Example: Implement an enumerator that utilizes an indexer:
 <code csharp> <code csharp>
 class Mapping class Mapping
Line 106: Line 97:
         get         get
         {         {
-            // Here goes a complicated ​algorithm to retrieve a value by the index...+            // Here goes a complex ​algorithm to retrieve a value by the index...
             return arr[i];             return arr[i];
         }         }
Line 120: Line 111:
     }     }
 } }
- 
 ... ...
- 
 Mapping collection = new Mapping(); Mapping collection = new Mapping();
  
Line 132: Line 121:
  
  
-====References====+====Links====
  
   * MSDN: [[https://​msdn.microsoft.com/​en-us/​library/​dscyy5s0.aspx|Iterators (C# and Visual Basic)]]   * MSDN: [[https://​msdn.microsoft.com/​en-us/​library/​dscyy5s0.aspx|Iterators (C# and Visual Basic)]]
   * MSDN Magazine (March 2007): [[http://​blogs.msdn.com/​b/​wesdyer/​archive/​2007/​03/​23/​all-about-iterators.aspx|All About Iterators]]   * MSDN Magazine (March 2007): [[http://​blogs.msdn.com/​b/​wesdyer/​archive/​2007/​03/​23/​all-about-iterators.aspx|All About Iterators]]
  
notes/csharp/yield.txt · Last modified: 2016/12/14 by admin