# Collections\_Generic\_IEnumerable\_T

***

**1. Iterating Over a Collection with foreach**

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

**2. Using LINQ to Query a Collection**

```csharp
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
```

**3. Converting an IEnumerable to an Array**

```csharp
int[] numbersArray = numbers.ToArray();
```

**4. Converting an IEnumerable to a List**

```csharp
List<int> numbersList = numbers.ToList();
```

**5. Filtering an IEnumerable with Where**

```csharp
var evenNumbers = numbers.Where(n => n % 2 == 0);
```

**6. Selecting a Value from an IEnumerable with Select**

```csharp
var numberNames = numbers.Select(n => "Number " + n);
```

**7. Grouping an IEnumerable with GroupBy**

```csharp
var numberGroups = numbers.GroupBy(n => n % 2 == 0);
```

**8. Joining Two IEnumerables with Join**

```csharp
var productSales = products.Join(sales, p => p.Id, s => s.ProductId, (p, s) => new { Product = p, Sale = s });
```

**9. Ordering an IEnumerable with OrderBy**

```csharp
var sortedNumbers = numbers.OrderBy(n => n);
```

**10. Reversing an IEnumerable with Reverse**

```csharp
var reversedNumbers = numbers.Reverse();
```

**11. Skipping Elements in an IEnumerable with Skip**

```csharp
var namesWithoutFirst = names.Skip(1);
```

**12. Taking Elements in an IEnumerable with Take**

```csharp
var firstThreeNames = names.Take(3);
```

**13. Concatenating IEnumerables with Concat**

```csharp
var allNames = names1.Concat(names2);
```

**14. Distinct Elements in an IEnumerable with Distinct**

```csharp
var uniqueNames = names.Distinct();
```

**15. Union Elements in IEnumerables with Union**

```csharp
var allNames = names1.Union(names2);
```

**16. Except Elements in IEnumerables with Except**

```csharp
var namesOnlyInList1 = names1.Except(names2);
```

**17. Intersect Elements in IEnumerables with Intersect**

```csharp
var commonNames = names1.Intersect(names2);
```

**18. Zipping Elements in IEnumerables with Zip**

```csharp
var nameAgePairs = names.Zip(ages, (name, age) => new { Name = name, Age = age });
```

**19. Basic Implementation of an IEnumerable**

```csharp
class MyEnumerable : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        yield return 1;
        yield return 2;
        yield return 3;
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
```

**20. Converting an Array to an IEnumerable**

```csharp
int[] numbers = { 1, 2, 3 };
IEnumerable<int> numbersEnumerable = numbers;
```

**21. Converting a List to an IEnumerable**

```csharp
List<int> numbers = new List<int> { 1, 2, 3 };
IEnumerable<int> numbersEnumerable = numbers;
```

**22. Iterating Over a Dictionary with foreach**

```csharp
foreach (var kvp in dictionary)
{
    Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
```

**23. Using LINQ to Query a Dictionary**

```csharp
var namesDictionary = new Dictionary<int, string>();
var namesWithA = namesDictionary.Where(kvp => kvp.Value.StartsWith("A")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
```

**24. Converting a Dictionary to an IEnumerable of Keys**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
IEnumerable<int> keys = dictionary.Keys;
```

**25. Converting a Dictionary to an IEnumerable of Values**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
IEnumerable<string> values = dictionary.Values;
```

**26. Filtering a Dictionary with Where**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var namesWithA = dictionary.Where(kvp => kvp.Value.StartsWith("A"));
```

**27. Selecting a Value from a Dictionary with Select**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var nameValues = dictionary.Select(kvp => kvp.Value);
```

**28. Grouping a Dictionary with GroupBy**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var namesByFirstLetter = dictionary.GroupBy(kvp => kvp.Value[0]);
```

**29. Joining Two Dictionaries with Join**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, double> dict2 = new Dictionary<int, double>();
var joined = dict1.Join(dict2, kvp1 => kvp1.Key, kvp2 => kvp2.Key, (kvp1, kvp2) => new { Key = kvp1.Key, Name = kvp1.Value, Amount = kvp2.Value });
```

**30. Ordering a Dictionary with OrderBy**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var sortedDictionary = dictionary.OrderBy(kvp => kvp.Value);
```

**31. Reversing a Dictionary with Reverse**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var reversedDictionary = dictionary.Reverse();
```

**32. Skipping Elements in a Dictionary with Skip**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var namesWithoutFirst = dictionary.Skip(1);
```

**33. Taking Elements in a Dictionary with Take**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var firstThreeNames = dictionary.Take(3);
```

**34. Concatenating Dictionaries with Concat**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, string> dict2 = new Dictionary<int, string>();
var combinedDictionary = dict1.Concat(dict2);
```

**35. Distinct Elements in a Dictionary with Distinct**

```csharp
Dictionary<int, string> dictionary = new Dictionary<int, string>();
var uniqueNames = dictionary.Select(kvp => kvp.Value).Distinct();
```

**36. Union Elements in Dictionaries with Union**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, string> dict2 = new Dictionary<int, string>();
var combinedDictionary = dict1.Union(dict2);
```

**37. Except Elements in Dictionaries with Except**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, string> dict2 = new Dictionary<int, string>();
var namesOnlyInDict1 = dict1.Except(dict2);
```

**38. Intersect Elements in Dictionaries with Intersect**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, string> dict2 = new Dictionary<int, string>();
var commonNames = dict1.Intersect(dict2);
```

**39. Zipping Elements in Dictionaries with Zip**

```csharp
Dictionary<int, string> dict1 = new Dictionary<int, string>();
Dictionary<int, double> dict2 = new Dictionary<int, double>();
var nameAmountPairs = dict1.Zip(dict2, (kvp1, kvp2) => new { Name = kvp1.Value, Amount = kvp2.Value });
```

**40. Basic Implementation of an IEnumerator**

```csharp
class MyEnumerator : IEnumerator<int>
{
    private int[] _numbers;
    private int _index = -1;

    public MyEnumerator(int[] numbers) => _numbers = numbers;

    public int Current => _numbers[_index];

    public bool MoveNext() => ++_index < _numbers.Length;

    public void Reset() => _index = -1;

    public void Dispose() { }
}
```

**41. Using an Enumerator with foreach**

```csharp
var numbers = new int[] { 1, 2, 3 };
foreach (var number in numbers)
{
    Console.WriteLine(number);
}
```

**42. Using an Enumerator with a while Loop**

```csharp
var numbers = new int[] { 1, 2, 3 };
IEnumerator<int> enumerator = numbers.GetEnumerator();

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

**43. Implementing an IEnumerable with an Enumerator**

```csharp
class MyEnumerableWithEnumerator : IEnumerable<int>
{
    private int[] _numbers;

    public MyEnumerableWithEnumerator(int[] numbers) => _numbers = numbers;

    public IEnumerator<int> GetEnumerator() => new MyEnumerator(_numbers);

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
```

**44. Iterating Over a String**

```csharp
string str = "Hello World!";
foreach (char c in str)
{
    Console.Write(c);
}
```

**45. Using LINQ to Query a String**

```csharp
string str = "Hello World!";
var vowels = str.Where(c => "AEIOUaeiou".Contains(c));
```

**46. Converting a String to an IEnumerable**

```csharp
string str = "Hello World!";
IEnumerable<char> characters = str;
```

**47. Filtering a String with Where**

```csharp
string str = "Hello World!";
var vowels = str.Where(c => "AEIOUaeiou".Contains(c));
```

**48. Selecting a Character from a String with Select**

```csharp
string str = "Hello World!";
var characterCodes = str.Select(c => (int)c);
```

**49. Grouping a String with GroupBy**

```csharp
string str = "Hello World!";
var characterGroups = str.GroupBy(c => c);
```

**50. Joining Two Strings with Join**

```csharp
string str1 = "Hello";
string str2 = "World";
string combinedString = string.Join(" ", str1, str2);
```
