|
| 1 | +using System.Collections; |
| 2 | + |
| 3 | +Console.WriteLine(); |
| 4 | + |
| 5 | +#region Changing Collection's Elements During Iteration - Exception |
| 6 | +/* |
| 7 | +List<int> numbers = Enumerable.Range(0,15).ToList(); |
| 8 | +foreach (int i in numbers) |
| 9 | +{ |
| 10 | + Console.WriteLine(i); |
| 11 | + numbers.Add(100); // This will cause an exception because the collection is modified during iteration. |
| 12 | + // Error: : 'Collection was modified; enumeration operation may not execute.' |
| 13 | +} |
| 14 | +*/ |
| 15 | +#endregion |
| 16 | + |
| 17 | +#region How to Make a Class Iterable |
| 18 | +/* |
| 19 | +Stock stock = new(); |
| 20 | +foreach (string material in stock) // Error: 'Stock' does not contain a definition for 'GetEnumerator' and no accessible extension method 'GetEnumerator' accepting a first argument of type 'Stock' could be found (are you missing a using directive or an assembly reference?) |
| 21 | +{ |
| 22 | + Console.WriteLine(material); |
| 23 | +} |
| 24 | +
|
| 25 | +
|
| 26 | +class Stock |
| 27 | +{ |
| 28 | + List<string> materials = new() { "Wood", "Steel", "Plastic", "Glass" }; |
| 29 | + public void Add(string material) => materials.Add(material); |
| 30 | +
|
| 31 | + public IEnumerator<string> GetEnumerator() // This method is required to make the class iterable |
| 32 | + { |
| 33 | + return materials.GetEnumerator(); |
| 34 | + } |
| 35 | +} |
| 36 | +*/ |
| 37 | +#endregion |
| 38 | + |
| 39 | +#region IEnumerable Interface |
| 40 | +/* |
| 41 | +// IEnumerable interface is required to make a class iterable (foreach loop), it has only one method GetEnumerator() and there is also generic version IEnumerable<T> |
| 42 | +
|
| 43 | +public class Stock : IEnumerable<string> |
| 44 | +{ |
| 45 | + List<string> materials = new() { "Wood", "Steel", "Plastic", "Glass" }; |
| 46 | + public void Add(string material) => materials.Add(material); |
| 47 | +
|
| 48 | + public IEnumerator<string> GetEnumerator() |
| 49 | + { |
| 50 | + return materials.GetEnumerator(); |
| 51 | + } |
| 52 | +
|
| 53 | + IEnumerator IEnumerable.GetEnumerator() // Non-generic version is implemented because object can be used in foreach loop |
| 54 | + { |
| 55 | + return materials.GetEnumerator(); |
| 56 | + } |
| 57 | +} |
| 58 | +*/ |
| 59 | +#endregion |
| 60 | + |
| 61 | +#region IEnumerator Interface |
| 62 | +/* |
| 63 | +class StockEnumerator : IEnumerator<string> |
| 64 | +{ |
| 65 | + List<string> source; |
| 66 | + int currentIndex = -1; |
| 67 | + public StockEnumerator(List<string> source) => this.source = source; |
| 68 | +
|
| 69 | +
|
| 70 | + public string Current => source[currentIndex]; |
| 71 | +
|
| 72 | + object IEnumerator.Current => source[currentIndex]; |
| 73 | +
|
| 74 | + public void Dispose() => source = null; |
| 75 | + public bool MoveNext() => ++currentIndex < source.Count; |
| 76 | + public void Reset() => source.Clear(); |
| 77 | +} |
| 78 | +*/ |
| 79 | +#endregion |
| 80 | + |
| 81 | +#region yield Keyword |
| 82 | +foreach (string name in GetNames()) |
| 83 | +{ |
| 84 | + Console.WriteLine(name); |
| 85 | +} |
| 86 | +IEnumerable GetNames() |
| 87 | +{ |
| 88 | + yield return "Hakan"; |
| 89 | + Console.WriteLine("After Hakan"); |
| 90 | + yield return "Alperen"; |
| 91 | + Console.WriteLine("After Alperen"); |
| 92 | + yield return "Mehmet"; |
| 93 | + Console.WriteLine("After Mehmet"); |
| 94 | + yield return "Ali"; |
| 95 | + Console.WriteLine("After Ali"); |
| 96 | + yield return "Veli"; |
| 97 | + Console.WriteLine("After Veli"); |
| 98 | +} |
| 99 | + |
| 100 | +#endregion |
| 101 | + |
0 commit comments