Read-Only Collections ^2

.NET already provides a read-only collection type with its System.Collections.Generic.ReadOnlyCollection<T>, but it’s just a single class, deceivingly named like a collection when it is actually wrapping and exposing an IList<T>:

UML diagram of a ReadOnlyCollection in .NET

I wasn’t quite happy with that. There are cases when you want to expose an ICollection<T> as read-only and copying the whole shebang into a list would be a waste of time and resources. So I spent some time implementing my own set of read-only collections, each one fully implementing all standard .NET collection interfaces – no corners cut:

ReadOnlyCollection<T> and ReadOnlyList<T>

Exactly what their names say, read-only wrappers for the ICollection<T> and IList<T> interfaces.

UML diagram of the Nuclex ReadOnlyCollection and ReadOnlyList

ReadOnlyDictionary<K, T> and ReadOnlySet<K>

Again, no surprise, these are read-only wrappers for the IDictionary<K, T> and ISet<K> interfaces.

UML diagram of the Nuclex ReadOnlyDictionary

Download

Nuclex.ReadOnlyCollection.Demo.7z (10 KiB)

(Contains the source code of all read-only collection classes with an example demonstrating their usage)