IEnumerator<T>
实现IDisposable
,如您在对象浏览器或 MSDN 中所见。
非泛型IEnumerator
没有。
基Array
类实现IEnumerable
但不实现IEnumerable<T>
。(因为Array
不是通用的)
具体的数组类型确实实现了IEnumerable<T>
,但它们GetEnumerator()
显式地实现了(我不确定为什么)。
因此,GetEnumerator()
任何数组类型上的可见都返回IEnumerator
.
通用IEnumerable<T>
实现返回一个System.SZArrayHelper.SZGenericArrayEnumerator<T>
.
此类 (in Array.cs
) 的源代码具有以下注释,部分解释了这一点(请记住,对泛型数组的所有支持都可以追溯到IEnumerable<T>
不违反规则的时代)
//---------------------------------------------------------------------------------------
// ! READ THIS BEFORE YOU WORK ON THIS CLASS.
//
// The methods on this class must be written VERY carefully to avoid introducing security holes.
// That's because they are invoked with special "this"! The "this" object
// for all of these methods are not SZArrayHelper objects. Rather, they are of type U[]
// where U[] is castable to T[]. No actual SZArrayHelper object is ever instantiated. Thus, you will
// see a lot of expressions that cast "this" "T[]".
//
// This class is needed to allow an SZ array of type T[] to expose IList<T>,
// IList<T.BaseType>, etc., etc. all the way up to IList<Object>. When the following call is
// made:
//
// ((IList<T>) (new U[n])).SomeIListMethod()
//
// the interface stub dispatcher treats this as a special case, loads up SZArrayHelper,
// finds the corresponding generic method (matched simply by method name), instantiates
// it for type <T> and executes it.
//
// The "T" will reflect the interface used to invoke the method. The actual runtime "this" will be
// array that is castable to "T[]" (i.e. for primitivs and valuetypes, it will be exactly
// "T[]" - for orefs, it may be a "U[]" where U derives from T.)
//---------------------------------------------------------------------------------------