How to get the index of the current iteration of a foreach loop in C#?
Hi everyone! I'm just starting to learn C# and I've encountered a problem: I need not only to iterate through the elements of a collection, but also to know the ordinal number of each element during the iteration. For example, I have a list of strings:
```csharp
List fruits = new List { "apple", "banana", "orange", "pear" };
```
I want to output each fruit with its index, like this:
```
0: apple
1: banana
2: orange
3: pear
```
Obviously, a `for` loop is perfect for this task:
```csharp
for (int i = 0; i < fruits.Count; i++)
{
Console.WriteLine($"{i}: {fruits[i]}");
}
```
But I'm specifically interested in `foreach`. I've heard that `foreach` is simpler and safer for reading, but it doesn't have a built-in counter variable. I tried doing this:
```csharp
int index = 0;
```