- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有这个代码...
var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
b[2] = 3;
ReadOnlyCollection<T>
实现
IList<T>
和
this[T]
在
IList<T>
中有一个二传手界面。
this[T]
中删除了 setter是编译错误。
最佳答案
indexer是通过显式接口(interface)实现实现的,因此您只有在执行以下操作时才能访问它:
IList<int> b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
b[2] = 3;
var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
((IList<int>)b)[2] = 3;
ReadOnlyCollection
时,您无法使用不受支持的部分功能,从而帮助您避免执行时间故障。
ReadOnlyCollection<T>
实际上显式地实现了整个索引器,但也提供了一个公共(public)的只读索引器。换句话说,它是这样的:
T IList<T>.this[int index]
{
// Delegate interface implementation to "normal" implementation
get { return this[index]; }
set { throw new NotSupportedException("Collection is read-only."); }
}
public T this[int index]
{
get { return ...; }
}
关于.net - ReadOnlyCollection 有什么神奇之处吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409854/
我不知道这是否可行,但基本上,我想公开一个 ReadOnlyCollection> 类型的属性,同时仍然能够在公开属性的类中修改基础集合。此外,我希望我的类的用户能够持有对返回集合的引用,以便它在我在
ReadOnlyCollection只支持读操作。为什么 T未标有 out关键字? 最佳答案 ReadOnlyCollection supports only reading operations 它
我想知道是否有人能指出我正确的方向...... 我如何从 ReadOnlyCollection 转换至 ReadOnlyCollection没有迭代或“更新”副本? 我正在使用 .NET Framew
这个问题在这里已经有了答案: Do not nest generic types in member signatures (1 个回答) 关闭 6 年前。 我正在修改我的所有代码以符合 FxCop
我有一个包含集合( Collection myItems )的类。我希望能够私下修改此 Collection 并通过公共(public)属性将其作为只读返回(如果可能,在 O(1) 中)。我正在考虑使
有这个代码... var b = new ReadOnlyCollection(new[] { 2, 4, 2, 2 }); b[2] = 3; 我在第二行收到编译错误。我预计会出现运行时错误,因为
我正在尝试使用 ReadOnlyCollection 使对象不可变,我希望对象的属性不可变。 public ReadOnlyCollection MyReadOnlyList { get
ReadOnlyCollection 构造函数要求您为它提供 IList。但是如果您有一些 ROC,您想要连接并生成一个新的 ROC,则 Concat 方法返回 IEnumerable。这不是传递给
在我的域对象中,我将 1:M 关系映射到 IList 属性。 为了更好的隔离,我这样设置为只读: private IList _property; public ReadOnlyCollection
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
MSDN含糊地提到: A ReadOnlyCollection)>) can support multiple readers concurrently, as long as the collect
假设一个方法看起来像这样。 Class MyClass public string ConcatenateList(ReadOnlyCollection aList) { var result =
我有一个具有 ReadOnlyCollection 的重线程应用程序,如下所示: internal static ReadOnlyCollection DistributorBackpressure4
我正在设计一些接口(interface)来使用 ReadOnlyCollection表明这些接口(interface)的使用者只能从集合中读取。 然而,所提供的集合并不是一成不变的,并且会不时发生变化
使用下面的代码我得到一个 CA2104 (DoNotDeclareReadOnlyMutableReferenceTypes) warning public readonly ReadOnlyColl
SO 上已经有几个类似的问题,但我发现没有一个真正涉及到这个特定主题,所以这里... 我的理解是,应该始终尝试通过具体类返回接口(interface)。不会深入探讨其背后的原因,已经有很多关于它的事情
此代码之所以有效,是因为 Enumerator无法修改集合: var roList = new List() { "One", "Two", "Three" }; IEnumerable objEnu
我使用 EntityFramewotk 和代码优先方法。所以,我这样描述我的模型: class Person { public long Id { get;set; } public
我有以下属性: private static Collection _loadedAssemblies = new Collection(); internal static ReadOnly
看来我做不到: private int[,] _table = new int[9, 9]; private ReadOnlyCollection _tableReadOnly = new ReadO
我是一名优秀的程序员,十分优秀!