C# Iterators5 Sept 2024 | 2 min read C# 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. 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 2Output: Rohan Peter Irfan Sohan Next TopicC# Nullable |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India