在.NET Framework 3.5(C#3.0)中,为什么System.Hashtable
实现了ICollection
和IEnumerable
当它已经实现IDictionary
继承这两个接口?为什么Hashtable实现ICollection和IEnumerable?
回答
由于直接基类的Hashtable
是System.Object
它实现了无接口,接口重新实现不能与此有关。
由于
IDictionary
不隐藏任何它从
IEnumerable
继承部件(例如,它不声明
new
方法
GetEnumerator()
),得到为具有相同名称的部件不同的实现也可以不是原因。
既然“惯常”的原因都不适用,我认为Hashtable
没有特别的原因。
编辑:
不知道这是连一个答案。
上面的陈述其实是错误的。非通用IDictionary
类型确实隐藏它从它的基本接口(非通用)IEnuemrable
之一继承的成员(超载)GetEnumerator()
。但不知道这是相关的。请参阅Hashtable
here和here中的两个实现。
新编辑:
其实,如果你查看源代码(发现如here),你看到的只是:
public class Hashtable : IDictionary, ISerializable, IDeserializationCallback, ICloneable
但作为一个线程写入an answer已链接到由Henrik在编译后的IL中发表了一个评论,所有的基本接口仍然被明确列出。因此,从编译程序集中的IL转换到伪C#的反编译器无法区分这种差异。
最终加入:
我真的想拿出涉及接口重新实现的情况下,如果你也给了一个已经指定派生接口的基础接口会的问题,但我不认为有任何存在。这是我的尝试:
但结果不是一个例子。即使只为Elephant
指定IDerived
,也会重新实现所有接口,包括IBase
。只有在Elephant
既不指定IDerived
也不指定IBase
时,才会重新执行压制。
所以默特的答案和相关的答案是正确的,我的观点有点偏离主题。我会离开这个帖子让其他人学习,即使它与提出的问题没有严格的相关性。
说完这些intefaces:
interface IFoo1 {...}
interface IFoo2: IFoo1{...}
有如下之间的编纂方面没有差别:
class MyClass: IFoo2{...}
class MyClass: IFoo2, IFoo1{...}
第二个声明使得它更清楚开发商所有接口MyClass的类实现。因此,查看文档并查看MyClass实现IFoo1接口更容易,而无需挖掘IFoo2接口。
- 1. 为什么ArrayList实现IList,ICollection,IEnumerable?
- 2. 为什么ICollection <T>实现IEnumerable <T>和IEnumerable
- 3. 在ICollection中实现IEnumerator和IEnumerable
- 4. WhereSelectArrayIterator为什么没有实现ICollection?
- 5. 集合<T>:它为什么既实现IEnumerable又实现IEnumerable <T>?
- 6. Hashtable实现
- 7. 为什么Hashtable不是PascalCase?
- 8. 什么是ICollection?
- 9. 为什么这个类不需要实现IEnumerable?
- 10. 为什么TreeNodeCollection不能实现IEnumerable <TreeNode>?
- 11. 为什么String类在便携库中实现IEnumerable <char>?
- 12. 为什么IEnumerable <T>实现Add(T)?
- 13. C++/CLI IEnumerable和IEnumerator实现
- 14. 在IEnumerable扩展中 - 为什么只有Count()为ICollection进行了优化?
- 15. 在NHibernate中使用IList,IEnumerable,ISet或ICollection有什么区别?
- 16. 了解ICollection实现和接口
- 17. 如何转换ICollection至IEnumerable?
- 18. 铸造的IEnumerable到ICollection的
- 19. Chunk IEnumerable/ICollection Class C#2.0
- 20. Datatable实现IEnumerable?
- 21. C++ HashTable对象实现
- 22. 为什么AbstractCollection实现了Iterable和Collection?
- 23. C#IEnumerable的实现
- 24. 如何实现IEnumerable?
- 25. 实现IEnumerable。错误。
- 26. 为什么ICollection不包含Add方法?
- 27. 为什么System.Net.Mail.MailMessage实现IDisposable
- 28. 为什么javax.servlet.GenericServlet实现javax.servlet.ServletConfig?
- 29. Enumerable.Range为什么实现IDisposable?
- 30. 为什么ServiceReference实现Comparable?
http://blogs.msdn.com/b/ericlippert/archive/2011/04/04/so-many-interfaces.aspx – BlackBear
该链接回答了与我的问题相关的所有内容。谢谢BlackBear。 – Tomas