C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can't directly get the current index of an item in a foreach
loop. However, there are several workarounds you can use to achieve this functionality.
for
loop: Instead of using a foreach
loop, you can use a for
loop and manually keep track of the current index:List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; for (int i = 0; i < numbers.Count; i++) { int currentNumber = numbers[i]; Console.WriteLine("Index: {0}, Number: {1}", i, currentNumber); }
Select
method: You can use the Select
method to create a new enumerable with the current index and the original item, and then use a foreach
loop on that enumerable:List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var indexedNumbers = numbers.Select((number, index) => new { Number = number, Index = index }); foreach (var indexedNumber in indexedNumbers) { Console.WriteLine("Index: {0}, Number: {1}", indexedNumber.Index, indexedNumber.Number); }
To determine which is the last iteration of the foreach
loop, you can use the Last
extension method on the enumerable to get the last item, or you can use a counter variable and compare it to the total number of items in the enumerable.
Iterating over arrays with foreach in C#:
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); }
Using foreach with collections in C#:
List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; foreach (string name in names) { Console.WriteLine(name); }
Iterating through dictionary entries using foreach in C#:
Dictionary<string, int> ages = new Dictionary<string, int> { { "Alice", 25 }, { "Bob", 30 } }; foreach (var entry in ages) { Console.WriteLine($"{entry.Key}: {entry.Value} years old"); }
Using foreach with strings in C#:
string message = "Hello, C#!"; foreach (char letter in message) { Console.WriteLine(letter); }
Looping through elements of a HashSet with foreach in C#:
HashSet<int> uniqueNumbers = new HashSet<int> { 1, 2, 3, 4, 5 }; foreach (int number in uniqueNumbers) { Console.WriteLine(number); }
Iterating over IEnumerable objects with foreach in C#:
IEnumerable<int> sequence = GetSomeNumbers(); foreach (int number in sequence) { Console.WriteLine(number); }
Applying foreach to custom collection classes in C#:
public class CustomCollection<T> : IEnumerable<T> { // Implementation details... public IEnumerator<T> GetEnumerator() { // Enumerator implementation... } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
Using foreach with multidimensional arrays in C#:
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } }; foreach (int number in matrix) { Console.WriteLine(number); }
Nested foreach loops in C#:
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } }; foreach (int row in matrix) { foreach (int number in row) { Console.WriteLine(number); } }
Breaking out of a foreach loop in C#:
break
.int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number == 3) break; Console.WriteLine(number); }
Skipping iterations in foreach with continue in C#:
continue
to skip iterations in a foreach loop.int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number % 2 == 0) continue; Console.WriteLine(number); }