问题描述
在 .NET 4.5/C# 5 中,IReadOnlyCollection
使用 Count
属性声明:
公共接口 IReadOnlyCollection;: IEnumerable, IEnumerable{int计数{得到;}}
我想知道,ICollection
也实现 IReadOnlyCollection
接口是否有意义:
公共接口ICollection: IEnumerable, IEnumerable, *IReadOnlyCollection*
这意味着实现 ICollection
的类会自动实现 IReadOnlyCollection
.这对我来说听起来很合理.
ICollection
抽象可以看作是 IReadOnlyCollection
抽象的扩展.请注意,例如,List
实现了 ICollection
和 IReadOnlyCollection
.
然而,它并不是这样设计的.
我在这里错过了什么?为什么会选择当前的实现?
更新
我正在寻找使用面向对象设计推理来解释原因的答案:
- 一个具体的类,例如
List
实现IReadOnlyCollection
和ICollection
>
比以下更好的设计:
ICollection
直接实现IReadOnlyCollection
另请注意,这与以下问题基本相同:
- 为什么
IList
没有实现IReadOnlyList
? - 为什么
IDictionary
没有实现IReadOnlyDictionary
?
Jon 就在这里 https://stackoverflow.com/a/12622784/395144 ,你应该把他的回复标记为答案:
int ICollection.Count { ... }//编译器错误!
由于接口可以有显式实现,提取基接口不向后兼容(使用基类你没有这个问题).
这就是为什么...
集合: IReadOnlyCollection列表: IReadOnlyList字典: IReadOnlyDictionary
...但不是他们的接口.
恕我直言,他们最初犯了一个设计错误,现在无法解决(不会破坏事物).
隐藏没有帮助,旧的(显式)实现仍然不会构建(不修改代码):
interface INew{ T 获取();}接口IOld <T>:INew<T>{无效集(T值);新 T 获取();}老式<T>:IOld T{T IOld.Get() { 返回默认值(T);}void IOld.Set(T value) { }}
'Sample.Old' 没有实现接口成员 'Sample.INew.Get()'
In .NET 4.5 / C# 5, IReadOnlyCollection<T>
is declared with a Count
property:
public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
}
I am wondering, wouldn't it have made sense for ICollection<T>
to implement the IReadOnlyCollection<T>
interface as well:
public interface ICollection<T> : IEnumerable<T>, IEnumerable, *IReadOnlyCollection<T>*
This would've meant that classes implementing ICollection<T>
would've automatically implemented IReadOnlyCollection<T>
. This sounds reasonable to me.
The ICollection<T>
abstraction can be viewed as an extension of the IReadOnlyCollection<T>
abstraction. Note that List<T>
, for example, implements both ICollection<T>
and IReadOnlyCollection<T>
.
However it has not been designed that way.
What am I missing here? Why would the current implementation have been chosen instead?
UPDATE
I'm looking for an answer that uses Object Oriented design reasoning to explain why:
- A concrete class such as
List<T>
implementing bothIReadOnlyCollection<T>
andICollection<T>
is a better design than:
ICollection<T>
implementingIReadOnlyCollection<T>
directly
Also please note that this is essentially the same question as:
- Why doesn't
IList<T>
implementIReadOnlyList<T>
? - Why doesn't
IDictionary<T>
implementIReadOnlyDictionary<T>
?
Jon was right here https://stackoverflow.com/a/12622784/395144 , you should mark his reply as the answer:
int ICollection<Foo>.Count { ... } // compiler error!
Since interfaces can have explicit implementations, extracting base interfaces is not backward compatible (with base classes you don't have this problem).
That's why...
Collection<T> : IReadOnlyCollection<T>
List<T> : IReadOnlyList<T>
Dictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
... but not their interfaces.
IMHO, they did a design error initially, quite unresolvable now (without breaking things).
EDIT: hiding doesn't help, old (explicit) implementations won't still build (without modifying the code):
interface INew<out T> { T Get(); }
interface IOld<T> : INew<T>
{
void Set(T value);
new T Get();
}
class Old<T> : IOld<T>
{
T IOld<T>.Get() { return default(T); }
void IOld<T>.Set(T value) { }
}
这篇关于为什么通用 ICollection 不在 .NET 4.5 中实现 IReadOnlyCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!