# Collections\_Generic\_IEnumerator\_T

***

**1. Iterate over a list of objects:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

foreach (string color in colors)
{
    Console.WriteLine(color);
}
```

**2. Iterate over a dictionary of key-value pairs:**

```csharp
Dictionary<int, string> people = new Dictionary<int, string>();
people.Add(1, "John");
people.Add(2, "Mary");
people.Add(3, "Bob");

foreach (KeyValuePair<int, string> person in people)
{
    Console.WriteLine($"{person.Key}: {person.Value}");
}
```

**3. Iterate over an array of elements:**

```csharp
int[] numbers = { 1, 2, 3, 4, 5 };

foreach (int number in numbers)
{
    Console.WriteLine(number);
}
```

**4. Iterate over a custom collection class:**

```csharp
public class MyCollection<T> : IEnumerable<T>
{
    private List<T> items;

    public MyCollection()
    {
        items = new List<T>();
    }

    public void Add(T item)
    {
        items.Add(item);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

MyCollection<string> myCollection = new MyCollection<string>();
myCollection.Add("Item 1");
myCollection.Add("Item 2");
myCollection.Add("Item 3");

foreach (string item in myCollection)
{
    Console.WriteLine(item);
}
```

**5. Use `GetEnumerator` to get the enumerator:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

IEnumerator<string> enumerator = colors.GetEnumerator();

while (enumerator.MoveNext())
{
    string color = enumerator.Current;
    Console.WriteLine(color);
}
```

**6. Use `GetEnumerator()` to get the enumerator for a key-value pair:**

```csharp
Dictionary<int, string> people = new Dictionary<int, string>();
people.Add(1, "John");
people.Add(2, "Mary");
people.Add(3, "Bob");

IEnumerator<KeyValuePair<int, string>> enumerator = people.GetEnumerator();

while (enumerator.MoveNext())
{
    KeyValuePair<int, string> person = enumerator.Current;
    Console.WriteLine($"{person.Key}: {person.Value}");
}
```

**7. Use `GetEnumerator()` to get the enumerator for an array:**

```csharp
int[] numbers = { 1, 2, 3, 4, 5 };

IEnumerator<int> enumerator = numbers.GetEnumerator();

while (enumerator.MoveNext())
{
    int number = enumerator.Current;
    Console.WriteLine(number);
}
```

**8. Use `GetEnumerator()` to get the enumerator for a custom collection class:**

```csharp
public class MyCollection<T> : IEnumerable<T>
{
    private List<T> items;

    public MyCollection()
    {
        items = new List<T>();
    }

    public void Add(T item)
    {
        items.Add(item);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

MyCollection<string> myCollection = new MyCollection<string>();
myCollection.Add("Item 1");
myCollection.Add("Item 2");
myCollection.Add("Item 3");

IEnumerator<string> enumerator = myCollection.GetEnumerator();

while (enumerator.MoveNext())
{
    string item = enumerator.Current;
    Console.WriteLine(item);
}
```

**9. Iterate over a collection using `foreach` and a local variable:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

foreach (string color in colors.ToList())
{
    Console.WriteLine(color);
}
```

**10. Iterate over a collection using `foreach` and a range variable:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

foreach (string color in Enumerable.Range(0, colors.Count))
{
    Console.WriteLine(color);
}
```

**11. Iterate over a collection using `foreach` and a lambda expression:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

foreach (string color in colors.Where(x => x.StartsWith("R")))
{
    Console.WriteLine(color);
}
```

**12. Iterate over a collection using `foreach` and a query expression:**

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

foreach (string color in from color in colors where color.StartsWith("R") select color)
{
    Console.WriteLine(color);
}
```

\*\*13. Iterate over a collection using a `for` loop and `GetEnumerator()`:

```csharp
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

IEnumerator<string> enumerator = colors.GetEnumerator();

for (; enumerator.MoveNext(); )
{
    string color

```
