Get current index of a foreach loop in C#
This post will discuss how to get the current index of a foreach loop in C#.
There are several ways to get the index of the current iteration of a foreach loop. The foreach loop in C# doesn’t have a built-in index. You can maintain an explicit counter, starting with 0, and increment the counter by 1 in each iteration of the foreach loop. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 2, 5, 1, 7 }; int i = 0; foreach (int e in nums) { Console.WriteLine("Element " + e + " present at index " + i); i++; } } } |
Output:
Element 2 present at index 0
Element 5 present at index 1
Element 1 present at index 2
Element 7 present at index 3
The following program creates an extension method for the above code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; public static class Extensions { public static void Each<T>(this IEnumerable<T> enumerable, Action<T, int> action) { var i = 0; foreach (var e in enumerable) { action(e, i++); } } } public class Example { public static void Main() { List<int> nums = new List<int>() { 2, 5, 1, 7 }; nums.Each((e, i) => { Console.WriteLine("Element " + e + " present at index " + i); }); } } |
Alternatively, you can use a standard for-loop to easily get the index for each element as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 2, 5, 1, 7 }; for (int i = 0; i < nums.Count; i++) { Console.WriteLine("Element " + nums[i] + " present at index " + i); } } } |
The following solution creates an anonymous object for every element in the collection by using an overload of LINQ’s Select. The Value property stores the original value in the collection, and the Index property stores the index of each value within the collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 2, 5, 1, 7 }; foreach (var it in nums.Select((e, i) => new { Value = e, Index = i })) { Console.WriteLine("Element " + it.Value + " present at index " + it.Index); } } } |
Finally, you can avoid heap allocations by using the following alternative syntax using ValueTuple starting with C#7:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> nums = new List<int>() { 2, 5, 1, 7 }; foreach (var (value, i) in nums.Select((value, i) => (value, i))) { Console.WriteLine("Element " + value + " present at index " + i); } } } |
That’s all about getting the current index of a foreach loop in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)