TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Iterators

C# Iterators for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Iterators

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 1

In this example, we are iterating array elements.

using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpFeatures
{
    class IteratorExample
    {
        public static IEnumerable GetArray()
        {
            int[] arr = new int[] { 5,8,6,9,1 };
            // Iterating array elements and returning
            foreach (var element in arr)
            {
                yield return element.ToString(); // It returns elements after executing each iteration
            }
        }
        public static void Main(string[] args)
        {
            // Storing elements, returned from the iterator method GetArray()
            IEnumerable elements = GetArray();
            foreach (var element in elements) // Iterating returned elements
            {
                Console.WriteLine(element);
            }
        }
    }
}

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 2

using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpFeatures
{
    class IteratorExample
    {
        public static IEnumerable GetList()
        {
            // List of string elements
            List list = new List();
            list.Add("Rohan"); // Adding elements
            list.Add("Peter");
            list.Add("Irfan");
            list.Add("Sohan");
            // Iterating and returing list elements
            foreach (var element in list)
            {
                yield return element; // It returns elements after executing each iteration
            }
        }
        public static void Main(string[] args)
        {
            // Storing elements, returned from the iterator method GetList()
            IEnumerable elements = GetList();
            foreach (var element in elements) // Iterating returned elements
            {
                Console.WriteLine(element);
            }
        }
    }
}

Output:

Rohan
Peter
Irfan
Sohan

Next TopicC# Nullable




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf