我知道在.NET中,所有数组都派生自System.Array并且System.Array类实现IList,ICollection和IEnumerable.实际的数组类型也实现IList<T>,ICollection<T>和IEnumerable<T>.
这意味着如果你有一个String[],那么那个String[]对象也是a System.Collections.IList和a System.Collections.Generic.IList<String>;.
不难看出为什么那些IList会被认为是"ReadOnly",但令人惊讶的是......
String[] array = new String[0];
Console.WriteLine(((IList<String>)array).IsReadOnly); // True
Console.WriteLine(((IList)array).IsReadOnly); // False!
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,尝试通过Remove()和RemoveAt()方法删除项会导致NotSupportedException.这表明两个表达式都对应于ReadOnly列表,但IList的ReadOnly属性不返回预期值.
怎么会?