【发布时间】:2014-01-28 01:24:23
【问题描述】:
在研究IEnumerator和IEnumerator<T>时,我遇到了以下说法:-
如果我们在任何集合上调用 GetEnumerator(),我们大多会得到类型安全的版本,即“通用”版本,在这种情况下值得注意的例外是返回经典(非通用)版本的数组。
我的问题是:-
为什么数组在调用GetEnumerator() 函数时返回“经典”枚举器,但其他数据结构如List<T> 和其他返回通用枚举器?
string[] tempString = "Hello World!" ;
var classicEnumerator = tempString.GetEnumerator() ; // non-generic version of enumerator.
List<string> tempList = new List<string> () ;
tempList.Add("Hello") ;
tempList.Add(" World") ;
var genericEnumerator = tempList.GetEnumerator() ; // generic version of enumerator.
【问题讨论】:
标签: c# .net enumerator ienumerator