Search code examples
c#listreadonly-collection

List and ReadOnly property


List (and List) instances can be readonly, seeing ReadOnly property; methods throws exceptions in the case the collection have the property ReadOnly property.

How can I create readonly List instances? What are the main uses?


Solution

  • ReadOnlyCollection

    From the link above:

    "A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes."

    So, anytime you need a collection that is not changeable, use this.