Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmodifiable lists in C#

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.

like image 477
Alex Marshall Avatar asked Nov 10 '09 20:11

Alex Marshall


1 Answers

ReadOnlyCollection

var dinosaurs = new List<string>();

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

var readOnlyDinosaurs = new ReadOnlyCollection<string>(dinosaurs);
like image 97
Bob Avatar answered Sep 22 '22 20:09

Bob