Junior Collections

What is the purpose of IReadOnlyCollection<T> and IReadOnlyList<T> interfaces?

These interfaces provide read-only access to collections:

  • IReadOnlyCollection<T> provides Count and enumeration.
  • IReadOnlyList<T> provides index-based access without modification.

Example:

IReadOnlyList<int> ids = new List<int> { 1, 2, 3 };

Console.WriteLine(ids[0]); // Access element

// ids[0] = 10; // Compile-time error – read-only

Real-world use case:

Useful in APIs where you want to expose collection data but prevent consumers from

modifying it—e.g., returning a list of supported currencies from a service.

Follow:

More from C# Programming Tutorial

All questions for this course