Skip to content

Commit c5b8f66

Browse files
pkulikovBillWagner
authored andcommitted
Added foreach keyword snippets (#78)
1 parent cb25543 commit c5b8f66

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

snippets/csharp/keywords/GenericWhereConstraints.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ class Test<T, U>
144144
{ }
145145
// </Snippet12>
146146

147-
// <Snippet13>
148-
public class List<T>
147+
namespace ListExample
149148
{
150-
public void Add<U>(List<U> items) where U : T {/*...*/}
149+
// <Snippet13>
150+
public class List<T>
151+
{
152+
public void Add<U>(List<U> items) where U : T {/*...*/}
153+
}
154+
// </Snippet13>
151155
}
152-
// </Snippet13>
153156

154157
// <Snippet14>
155158
//Type parameter V is used as a type constraint.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace keywords
5+
{
6+
public static class IterationKeywordsExamples
7+
{
8+
public static void Examples()
9+
{
10+
ForeachWithIEnumerable();
11+
ForeachWithSpan();
12+
}
13+
14+
private static void ForeachWithIEnumerable()
15+
{
16+
// <Snippet1>
17+
var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
18+
int count = 0;
19+
foreach (int element in fibNumbers)
20+
{
21+
count++;
22+
Console.WriteLine($"Element #{count}: {element}");
23+
}
24+
Console.WriteLine($"Number of elements: {count}");
25+
// </Snippet1>
26+
}
27+
28+
private static void ForeachWithSpan()
29+
{
30+
// <Snippet2>
31+
Span<int> numbers = new int[] { 3, 14, 15, 92, 6 };
32+
foreach (int number in numbers)
33+
{
34+
Console.Write($"{number} ");
35+
}
36+
Console.WriteLine();
37+
// </Snippet2>
38+
}
39+
}
40+
}

snippets/csharp/keywords/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ static void Main(string[] args)
1212
GenericWhereConstraints.Examples();
1313
Console.WriteLine("================= Fixed Memory Examples ======================");
1414
FixedKeywordExamples.Examples();
15+
Console.WriteLine("================= Iteration Keywords Examples ======================");
16+
IterationKeywordsExamples.Examples();
1517
}
1618
}
1719
}

0 commit comments

Comments
 (0)