本文介绍了为什么通用ICollection在.NET 4.5中实现IReadOnlyCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在.NET 4.5 / C#5中, IReadOnlyCollection< T> 被声明为一个 Count property: p> public interface IReadOnlyCollection< out T> :IEnumerable< T> ;, IEnumerable { int Count {get; } } 我想知道,对于 ICollection< T> 以实现 IReadOnlyCollection< T> 界面: public interface ICollection< T> :IEnumerable< T>,IEnumerable,* IReadOnlyCollection< T> * 这将意味着类实现 ICollection< T> 将自动实现 IReadOnlyCollection< T> 。这对我来说听起来很合理。 ICollection< T> 抽象可以被视为 IReadOnlyCollection< T> 抽象。请注意,例如, List< T> 实现 ICollection< T> 和 IReadOnlyCollection ; T> 。 然而,它并没有被设计成这样。 我在这里缺少什么?为什么会选择当前的实施方式? 更新 我正在寻找使用面向对象设计推理的答案来解释为什么: 一个具体的类,例如 List< T> 实现 IReadOnlyCollection< T> ICollection< T> 设计比 ICollection< T> 实现 IReadOnlyCollection< T> 直接 另外请注意,这基本上是一样的问题: 为什么不 IList< T> 实现 IReadOnlyList< T> ? 为什么不 IDictionary< T> code>实现 IReadOnlyDictionary< T> ? 解决方案 Jon正好在这里 http://stackoverflow.com/a/12622784/395144 ,你应该将他的回复标记为答案: int ICollection&Foo> .Count {...} / /编译错误! 由于接口可以有明确的实现,提取基础接口不是向后兼容的(使用基类,有这个问题)。 这就是为什么... 收集和LT; T> :IReadOnlyCollection< T> 列表< T> :IReadOnlyList< T> 词典< TKey,TValue> :IReadOnlyDictionary TKey,TValue> ...但不是他们的界面。 编辑:隐藏没有帮助,老(显式)仍然构建(不修改代码): interface INew< out T> {T Get(); } 接口IOld< T> :新< 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){} } '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?UPDATEI'm looking for an answer that uses Object Oriented design reasoning to explain why:A concrete class such as List<T> implementing both IReadOnlyCollection<T> and ICollection<T>is a better design than:ICollection<T> implementing IReadOnlyCollection<T> directlyAlso please note that this is essentially the same question as:Why doesn't IList<T> implement IReadOnlyList<T>?Why doesn't IDictionary<T> implement IReadOnlyDictionary<T>? 解决方案 Jon was right here http://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) { }} 'Sample.Old' does not implement interface member 'Sample.INew.Get()' 这篇关于为什么通用ICollection在.NET 4.5中实现IReadOnlyCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 15:57