Tracks
/
C#
C#
/
Syllabus
/
Readonly Collections
Re

Readonly Collections in C#

1 exercise

About Readonly Collections

While the readonly modifier prevents the value or reference in a field from being overwritten, it offers no protection for the members of a reference type.

readonly List<int> ints = new List<int>();

void Foo()
{
    ints.Add(1);    // ok
    ints = new List<int>(); // fails to compile
}

To ensure that all members of a reference type are protected the fields can be made readonly and automatic properties can be defined without a set accessor.

The Base Class Library (BCL) provides some readonly versions of collections where there is a requirement to stop members of a collections being updated. These come in the form of wrappers:

Edit via GitHub The link opens in a new window or tab

Learn Readonly Collections