Different languages implement iterators in different ways. Some object-oriented languages are like Java , C# , Ruby , Python , Delphi The features of iterators have been built into the language, which is perfectly integrated with the language. We call it implicit iterator, but like C++language, it has no iterator features, but STL still uses Template A powerful iterator is implemented. The memory address of STL container data may be reallocated. The iterator bound to the container can still locate the correct memory address after reallocation. Iterator can also be integrated generator (generator)。 Some languages regard the two as the same interface, and some languages such as JavaScript Then make it independent. [1] C♯
A new form of iterator that provides generator , use yield return [1] Similar to yield used in Python
// Method that takes an iterable input (possibly an array) // and returns all even numbers. public static IEnumerable<int> GetEven(IEnumerable<int> numbers) { foreach(int i in numbers) { if (i % 2 == 0) yield return i; } }
C♯