What is the difference between the non-generic IEnumerable and the generic IEnumerable?

后端 未结 5 1832
不思量自难忘°
不思量自难忘° 2020-12-31 15:34

Sorry for such a vague question, but I have been searching around for the best part of a day, I have read article after article (and many questions here) but just cannot fin

5条回答
  •  感情败类
    2020-12-31 16:15

    An IEnumerable has a GetEnumerator method which will return an IEnumerator, whose Current method will return an Object. An IEnumerable has a GetEnumerator method which will return an IEnumerator, whose Current method will return a T. If you know in advance the expected type of the object to be returned by the enumerator, it's generally better to use the generic form.

    Another distinction is that IEnumerator inherits IDisposable, which allows code which is done with an enumerator to call Dispose on it without having to worry about whether it is supported. By comparison, when using a non-generic IEnumerator, it's necessary to test whether it is IDisposable and call Dispose on it if so. Note that use of the non-generic form of IEnumerable/IEnumerator does not relieve one of the requirement to call Dispose. As an example, casting the vb-style Collection to IEnumerable and then calling GetEnumerator 100,000 times without calling Dispose will be extremely slow (many seconds, even on an i7) unless a garbage-collection happens to occur. Disposing the IEnumerator after each call will speed things up more than a hundredfold.

提交回复
热议问题