# Collections\_Generic\_IReadonlyCollection\_out\_T

***

**1. Get the count of elements in a read-only collection.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int count = numbers.Count;
```

**2. Check if a read-only collection contains a specific element.**

```csharp
IReadOnlyCollection<string> names = new List<string> { "John", "Mary", "Bob" };

bool containsJohn = names.Contains("John");
```

**3. Iterate over the elements in a read-only collection.**

```csharp
IReadOnlyCollection<char> letters = new List<char> { 'a', 'b', 'c', 'd', 'e' };

foreach (char letter in letters)
{
    Console.WriteLine(letter);
}
```

**4. Copy the elements of a read-only collection to an array.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int[] array = new int[numbers.Count];
numbers.CopyTo(array, 0);
```

**5. Create a new read-only collection from an existing collection.**

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

IReadOnlyCollection<int> readOnlyNumbers = new ReadOnlyCollection<int>(numbers);
```

**6. Use a read-only collection as a parameter to a method.**

```csharp
void PrintCollection<T>(IReadOnlyCollection<T> collection)
{
    foreach (T item in collection)
    {
        Console.WriteLine(item);
    }
}
```

**7. Use a read-only collection in a LINQ query.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

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

**8. Use a read-only collection in a lambda expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int sum = numbers.Sum(n => n);
```

**9. Use a read-only collection in a foreach loop.**

```csharp
IReadOnlyCollection<string> names = new List<string> { "John", "Mary", "Bob" };

foreach (string name in names)
{
    Console.WriteLine(name);
}
```

**10. Use a read-only collection in a switch statement.**

```csharp
IReadOnlyCollection<string> names = new List<string> { "John", "Mary", "Bob" };

switch (name)
{
    case "John":
        Console.WriteLine("Hello John!");
        break;
    case "Mary":
        Console.WriteLine("Hello Mary!");
        break;
    case "Bob":
        Console.WriteLine("Hello Bob!");
        break;
}
```

**11. Use a read-only collection in a ternary conditional expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int max = numbers.Count > 0 ? numbers.Max() : 0;
```

**12. Use a read-only collection in a null coalescing expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int count = numbers?.Count ?? 0;
```

**13. Use a read-only collection in a pattern matching expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

switch (numbers)
{
    case { Count: 0 }:
        Console.WriteLine("The collection is empty.");
        break;
    case { Count: 1 }:
        Console.WriteLine("The collection has one element.");
        break;
    default:
        Console.WriteLine("The collection has multiple elements.");
        break;
}
```

**14. Use a read-only collection in a nameof expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

string name = nameof(numbers);
```

**15. Use a read-only collection in a checked expression.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int sum = checked(numbers.Sum(n => n));
```

**16. Use a read-only collection in an unsafe context.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

unsafe
{
    fixed (int* p = numbers)
    {
        for (int i = 0; i < numbers.Count; i++)
        {
            Console.WriteLine(p[i]);
        }
    }
}
```

**17. Use a read-only collection in a lock statement.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

lock (numbers)
{
    // Do something with the collection.
}
```

**18. Use a read-only collection in a using statement.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

using (var enumerator = numbers.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        int number = enumerator.Current;

        // Do something with the number.
    }
}
```

**19. Use a read-only collection in a yield return statement.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

IEnumerable<int> GetNumbers()
{
    foreach (int number in numbers)
    {
        yield return number;
    }
}
```

**20. Use a read-only collection in a lambda expression that captures a variable.**

```csharp
IReadOnlyCollection<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int sum = numbers.Sum(n =>
{
    int

```
