【问题标题】:Which collection does not implement IEnumerable哪个集合没有实现 IEnumerable
【发布时间】:2016-05-10 09:48:21
【问题描述】:

System.Collections 下是否有任何类型的集合不继承自 IEnumerableIEnumerable<T>

我需要一个例子。

【问题讨论】:

  • 出于兴趣,为什么?
  • 我怀疑有一个集合没有实现IEnumerable
  • @DannyChen 即使您的命名空间具有相同的名称,它仍然是与 global::System.Collections 不同的命名空间;)
  • @Sean 为了您的兴趣,我正在阅读以下内容:“在 C# 中,集合类并非绝对需要从 IEnumerable 和 IEnumerator 继承才能与 foreach 兼容”msdn.microsoft.com/en-us/library/aa288462(v=vs.71).aspx 然后我想知道哪个集合不继承自IEnumerable

标签: c# .net collections ienumerable


【解决方案1】:

System.Collections 内的任何类直接 将实现 IEnumerable<T> - 该命名空间早于 .NET 中的泛型 - 但是可以在该命名空间中粗略定义为“集合”的类都实现 IEnumerable

  • 数组列表
  • 位数组
  • 集合库
  • 字典库
  • 哈希表
  • 队列
  • ReadOnlyCollectionBase
  • 排序列表
  • 堆栈

如果我们扩展为“System.Collections 以下的任何命名空间”,我们将进入 System.Collections.GenericSystem.Collections.ObjectModelSystem.Collections.Specialized - 其中的所有集合类都实现了 IEnumerableIEnumerable<T> 或两者。

【讨论】:

  • 还要注意,即使 System.Array 也实现了 IEnumerable。
  • 还字符串继承 IEnumerable(常见问题)。
猜你喜欢
  • 2013-11-12
  • 2011-02-22
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
相关资源
最近更新 更多