C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# IteratorsC# iterator is a method. It is used to iterate the elements of a collection, array or list. An iterator uses yield return statement to return each element at a time. The iterator remembers the current location and in next iteration, it returns the next element. The return type of an iterator can be IEnumerable<T> or IEnumerator<T>. To stop iteration, we can use yield break statement. C# Iterator Example 1In this example, we are iterating array elements. using System; using System.Collections.Generic; using System.Linq; namespace CSharpFeatures { class IteratorExample { public static IEnumerable Output: 5 8 6 9 1 Iterator can also be used to iterate collection elements. In the following example, we are iterating list elements. C# Iterator Example 2using System; using System.Collections.Generic; using System.Linq; namespace CSharpFeatures { class IteratorExample { public static IEnumerable Output: Rohan Peter Irfan Sohan
Next TopicC# Nullable
|