- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
当 IReadOnlyList<T>
是在 .NET 4.5 中引入的,有那么一刻我认为拼图的缺失部分终于被插入到位:一种传递真正的只读可索引接口(interface)的方法,以前我必须使用我自己的只读接口(interface)并创建包装类围绕一切。
我期望界面被放置在“自然”层次结构中,理想情况下是:
IEnumerable<T>
.GetEnumerator()
-> IReadOnlyCollection<T> : IEnumerable<T>
.Count
-> IReadOnlyList<T> : IReadOnlyCollection<T>
.Item[...]
-> IList<T> : IReadOnlyList<T>
.Add(...)
.Clear()
.Contains(...)
(etc)
但是,事实证明, IList<T>
不继承自 IReadOnlyList<T>
.
这有什么原因吗?
一些说明:
请注意 IReadOnlyList<T>
只是一个契约,它声明列表提供了一种获取列表计数并读取特定索引处的值的方法。它的命名很糟糕,因为它没有强制执行只读的实际实现。
A List<T>
工具 IEnumerable<T>
, 一个 IList<T>
继承自 IEnumerable<T>
,但这并不意味着这些类只能被枚举。
所以,如果你想将一个列表传递给一个方法,并且只允许它被索引(读取),而不是被修改,你需要将它包装在一个新的实例中。同时,您可以将其传递给接受 IEnumerable<T>
的方法。或 IList<T>
无需 包装它。这就是我发现的问题。
我也相信专有名称应该是 ICountable
对于 IReadOnlyCollection
和 IIndexable
对于 IReadOnlyList
:
IEnumerable<T>
.GetEnumerator()
-> ICountable<T> : IEnumerable<T>
.Count
-> IIndexable<T> : ICountable<T>
.Item[...]
-> IList<T> : IIndexable<T>
.Add(...)
.Clear()
.Contains(...)
(etc)
最佳答案
@w.b 放一个链接到New interfaces IReadOnlyList and IReadOnlyDictionary在包含答案的评论中:
Why did we not change the existing interfaces to extend the read-only interfaces?
It looks like a reasonable assumption that it works because the read-only interfaces are purely a subset of the read-write interfaces. Unfortunately, it is incompatible because at the metadata level every method on every interface has its own slot (which makes explicit interface implementations work).
Immo Landwerth | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/
为了更清楚地解释这一点:
假设为 .NET 4.0 编写的程序包含类 MyList<T>
实现 IList<T>
.它显然无法实现 IReadOnlyList<T>
因为该接口(interface)不存在。
现在假设系统管理员安装了 .NET 4.5 并假设 .NET 4.5 生成了 IList<T>
实现 IReadOnlyList<T>
.
如果程序随后被加载,运行时将检测到 MyList<T>
声称要执行IList<T>
, 但实际上并没有实现所有的方法:它没有实现 IReadOnlyList<T>
的方法。该程序将不再有效。
C# 编译器可能能够按名称匹配方法,但运行时不会这样做。由于 .NET 4.5 应该具有向后二进制兼容性,因此无法扩展接口(interface)以实现其他接口(interface),即使这些其他接口(interface)包含所需方法的严格子集也是如此。
关于c# - 为什么 `IList<T>` 不继承自 `IReadOnlyList<T>` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938995/
我正在尝试转换 IList>至 IList ,我的意思是有一个唯一的列表,其中包含第一个的所有元素(对象)。 public IList> PMTs { get
在 C# 中,如果我有一个 IList,但我不知道 IList 中对象的类型,我该如何创建一个 的副本>IList? 情况是这样的: 我有一个 CollectionEditor,它可以修改 IList
IList不继承 IList其中 IEnumerable继承IEnumerable . 如果out修饰符是大多数执行 IList 的唯一原因(例如 Collection , List )实现 ILis
尝试过: IList> matrix = new List()>(); 可是我做不到。我该怎么做?我需要一个字符串矩阵... 最佳答案 你需要: IList> matrix = new List>()
我有一个构建列表列表的方法。我想让返回类型使用通用 IList<> 接口(interface)来减少与下游具体 List<> 类型的耦合。但是,编译器在类型转换方面遇到了困难。 public ILis
我有一个 IList我想将此集合复制到 IList收藏 我试过这个: IList listAdminVAT = new AdministrationService(session).ListDecim
我有一个实现了 IList 的对象接口(interface),我想把它转换成IList或者 List ,我试过了 IList a=(IList)b; List a=(IList)b; IList a=
我似乎无法弄清楚为什么以下内容不起作用。 通过阅读,它似乎一定与 this 之类的东西有某种关联。 . public class Test { public void SomeFunction
我有几个类: class Vehicle { } class Car : Vehicle { } 我有一个派生类的列表: IList cars; 我想将列表转换为其基类,并尝试过: IList bas
List的定义在 .net 中显示它实现了各种接口(interface)。 public class List : IList, ICollection, IEnumerable, IList, IC
我想创建一个 ListCollectionView使用通用 IList但是构造函数得到一个非泛型 IList .如何将我的列表从 IList 转换为至 IList ? 如果有另一种方法可以创建 Lis
这个问题在这里已经有了答案: Remove items from one list in another (10 个答案) 关闭 8 年前。 很抱歉提出这样的基本问题,我是 LINQ 的新手,我正试
我正在实现 IListSource这需要一个方法 GetList()具有以下签名: IList GetList() 我正在使用 .NET Framework 2,我想返回一个实现 IList 的对象,
我有一个类(class)想要 IList , 但我有一个 Systems.Collection.IList , 来自 NHibernate 问题。 我想创建一个将其转换为 IList 的方法.我该怎么
我似乎找不到为我的 MVVM Xamarin 表单应用程序构建和使用数据的最佳实践或最佳机制。这可能是对如何解决问题的简单误解,但我不确定。 我曾尝试使用 IEnumerable、IList 和一个简
多年来,我们大部分时间都在使用泛型集合。有时我们确实需要任何东西的集合(好吧,通常只有一些不同的东西但没有公共(public)基类)。对于这种情况,我们可以使用 IList或通用 IList作为方法参
我有一个 IList目的。 Person 类有字段,FirstName , LastName等 我有一个函数需要 IList我想以与 IList 相同的顺序传入 FirstNames 列表目的。有没有
尝试这样转换 Products1 = (IList)basicProfile.Products2.Select(ToProductInfo) Products1 是一个 public IList Pr
我正在尝试创建一个列表列表,但在实例化列表时遇到了问题。 IList> allLists = List>(); 我遇到了这一行的编译错误。 最佳答案 你必须实例化一个 List的 IList , 不是
我有一个集合,它实现了一个扩展 IList 和 List 的接口(interface)。 public Interface IMySpecialCollection : IList, IList {
我是一名优秀的程序员,十分优秀!