Unmodifiable lists in C#

6k views Asked by At

In Java, one can use the Collections#unmodifiableList() method to create an unmodifiable list from an existing List object. Is there any counterpart in C# ? I'm new to the language and haven't been able to find anything like this in the MSDN docs.

1

There are 1 answers

4
Bob On BEST ANSWER

ReadOnlyCollection

var dinosaurs = new List<string>();

dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");

var readOnlyDinosaurs = new ReadOnlyCollection<string>(dinosaurs);