This post will discuss how to read-only, immutable, unmodifiable List in C#.

To get a read-only view of a list, we can use the ReadOnlyCollection<T> class. The following code example demonstrates its usage. It creates a List<T> of integers and then wraps the list in a ReadOnlyCollection<T>.

Download  Run Code

 
Note that the ReadOnlyCollection<T> class is a read-only view over the specified list. Therefore, any changes made to the underlying list are reflected in the read-only list.

 
Alternatively, you can use the List<T>.AsReadOnly() convenience method to get a read-only IList<T> generic interface implementation that wraps the original list.

Download  Run Code

That’s all about read-only, unmodifiable List in C#.