Skip to content

Commit 2a3b966

Browse files
Merge pull request #2 from hakanyavaseng/AP_17_Custom-Iterational-Classes-yield
AP 17 completed
2 parents 2cad17d + 7adb78a commit 2a3b966

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>_17_Custom_Iterational_Class_yield</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+

Advanced-C#-Programming/Advanced-C#-Programming.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "15_Access-Modifiers", "15_A
3131
EndProject
3232
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{17293333-6C24-4A93-9D57-B6DB72D68971}"
3333
EndProject
34-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "16_Predefined-Interfaces", "16_Predefined-Interfaces\16_Predefined-Interfaces.csproj", "{B30B1183-ED3B-4372-B6CF-E971288C1E57}"
34+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "16_Predefined-Interfaces", "16_Predefined-Interfaces\16_Predefined-Interfaces.csproj", "{B30B1183-ED3B-4372-B6CF-E971288C1E57}"
35+
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "17_Custom-Iterational-Class-yield", "17_Custom-Iterational-Class-yield\17_Custom-Iterational-Class-yield.csproj", "{E1119E66-3CC3-499A-AC30-1DD5DC3705E1}"
3537
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -99,6 +101,10 @@ Global
99101
{B30B1183-ED3B-4372-B6CF-E971288C1E57}.Debug|Any CPU.Build.0 = Debug|Any CPU
100102
{B30B1183-ED3B-4372-B6CF-E971288C1E57}.Release|Any CPU.ActiveCfg = Release|Any CPU
101103
{B30B1183-ED3B-4372-B6CF-E971288C1E57}.Release|Any CPU.Build.0 = Release|Any CPU
104+
{E1119E66-3CC3-499A-AC30-1DD5DC3705E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105+
{E1119E66-3CC3-499A-AC30-1DD5DC3705E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
106+
{E1119E66-3CC3-499A-AC30-1DD5DC3705E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
107+
{E1119E66-3CC3-499A-AC30-1DD5DC3705E1}.Release|Any CPU.Build.0 = Release|Any CPU
102108
EndGlobalSection
103109
GlobalSection(SolutionProperties) = preSolution
104110
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)