根据接口的实现Array.IsReadOnly不一致 [英] Array.IsReadOnly inconsistent depending on interface implementation
问题描述
一个类型数组同时实现了 System.Collections.IList
和了System.Collections.Generic.ICollection< T>
接口,其中两个有自己的的IsReadOnly
属性。但什么是怎么回事?
A typed array implements both the System.Collections.IList
and System.Collections.Generic.ICollection<T>
interfaces, which both have their own IsReadOnly
properties. But what on earth is going on here?
var array = new int[10];
Console.WriteLine(array.IsReadOnly); // prints "False"
var list = (System.Collections.IList)array;
Console.WriteLine(list.IsReadOnly); // prints "False"
var collection = (System.Collections.Generic.ICollection<int>)array;
Console.WriteLine(collection.IsReadOnly); // prints "True"
在的IList
查看阵列的行为就像我期望,返回相同的阵列本身,但是的ICollection&LT; T&GT;
鉴于阵列返回true。
The IList
view of the array behaves as I'd expect, returning the same as the array itself, however the ICollection<T>
view of the array returns true.
有没有合理的解释这种行为,或者是一个编译器/ CLR错误? (我会,如果是后者,你会想这将以前现在发现真的很奇怪,但它是如此反直觉我想不出什么原因,这可能...)。
Is there any rational explanation for this behaviour, or is it a compiler/CLR bug? (I'd be really surprised if it's the latter as you'd imagine this would have been found before now, but it's so counter-intuitive I can't think what the explanation could be...).
我使用C#3.0 / .NET 3.5 SP1。
I'm using C#3.0/.NET 3.5 SP1.
推荐答案
有大量的痛苦了这个决定,是明显的这个评论的反馈文章。
There was plenty of agony over this decision, as evident in the comments on this feedback article.
这篇关于根据接口的实现Array.IsReadOnly不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!