SoFunction
Updated on 2025-03-07

How to convert a List to read-only in C#


List<int> a = new List<int> {1, 2, 3, 4, 5}; 

IList<int> b = (); // block modification... 

IList<int> c = (); // ... but unblock it again 

(6); 
( == 6); // we've modified the original 

IEnumerable<int> d = (x => x); // okay, try this... 

IList<int> e = (); // no, can still get round it 

(7);