You're looking for ReadOnlyCollection
, which has been around since .NET2.
IList<string> foo = ...;
// ...
ReadOnlyCollection<string> bar = new ReadOnlyCollection<string>(foo);
or
List<string> foo = ...;
// ...
ReadOnlyCollection<string> bar = foo.AsReadOnly();
This creates a read-only view, which reflects changes made to the wrapped collection.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…