阅读有关C#(基本上,你不能这样做)创建一个只读基本向量的问题,为什么C#的System.Collections库中没有ReadOnlyList <T>类?
public readonly int[] Vector = new int[]{ 1, 2, 3, 4, 5 }; // You can still changes values
我了解ReadOnlyListBase。这是一个容器对象的基类,可以访问它们的位置但不能修改它们。即使在微软msdn的中也有一个例子。
http://msdn.microsoft.com/en-us/library/system.collections.readonlycollectionbase.aspx
我稍微修改了例子MSDN使用任何类型:
public class ReadOnlyList<T> : ReadOnlyCollectionBase {
public ReadOnlyList(IList sourceList) {
InnerList.AddRange(sourceList);
}
public T this[int index] {
get {
return((T) InnerList[ index ]);
}
}
public int IndexOf(T value) {
return(InnerList.IndexOf(value));
}
public bool Contains(T value) {
return(InnerList.Contains(value));
}
}
...和它的作品。我的问题是,为什么在C#的标准库中不存在这个类,可能在System.Collections.Generic中?我错过了吗?它在哪里? 谢谢。
更新2015年:.NET 4.5现在有ImmutableList https://msdn.microsoft.com/en-us/library/dn467185(v=vs.111).aspx – 2015-05-11 10:55:12