TheDeveloperBlog.com

Home | Contact Us

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

C# IEnumerable Examples: LINQ, Lists and Arrays

This C# tutorial demonstrates the IEnumerable interface. Example code with the foreach-loop is shown.

IEnumerable. A List, array, and query can be looped over.

This makes sense. All these constructs implement methods from IEnumerable.

An interface, IEnumerable specifies that the underlying type implements GetEnumerator. It enables foreach. On IEnumerable things, extensions from System.Linq are often applied.

Let us begin. An IEnumerable generic interface is returned from query expressions. A query expression that selects ints will be of type IEnumerable<int>.

Foreach: On an IEnumerable variable, we can also use the foreach-loop. This loop iterates with simple syntax.

Tip: We can apply many transformations to an IEnumerable instance, including the ToList and ToArray conversions.

ToListToArrayAverage

Based on:

.NET 4.5

C# program that uses IEnumerable

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
	IEnumerable<int> result = from value in Enumerable.Range(0, 2)
				  select value;

	// Loop.
	foreach (int value in result)
	{
	    Console.WriteLine(value);
	}

	// We can use extension methods on IEnumerable<int>
	double average = result.Average();

	// Extension methods can convert IEnumerable<int>
	List<int> list = result.ToList();
	int[] array = result.ToArray();
    }
}

Output

0
1

Enumerable.Range. Some methods, like Range(), make it easier to create IEnumerable collections. We do not need to create a separate array.

Enumerable.RangeEnumerable.Repeat

Arguments. Many classes implement IEnumerable. We can pass them directly to methods that receive IEnumerable arguments. The type parameter must be the same.

Next: Display() receives an IEnumerable argument. We can pass Lists or arrays to it.

Also: We can implement IEnumerable on a type to provide support for the foreach-loop. This is done through the GetEnumerator method.

C# program that uses IEnumerable argument

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	Display(new List<bool> { true, false, true });
    }

    static void Display(IEnumerable<bool> argument)
    {
	foreach (bool value in argument)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

True
False
True

2D array. IEnumerable works with a 2D array. It enables a foreach-loop over the values in a 2D or jagged array. We create a custom method that returns IEnumerable.

2D Array

Foreach: We use IEnumerable and the foreach-loop to access, in sequence, all items in a 2D array. We can abstract the loop itself out.

Here: This example shows the yield contextual keyword in a method that returns IEnumerable<T>.

Generic: The return value is a generic IEnumerable collection of ints. We must specify the int in angle brackets.

Generic ClassInt

C# program that uses array, yield keyword

using System;
using System.Collections.Generic;

class Program
{
    static int[,] _grid = new int[15, 15];
    static void Main()
    {
	_grid[0, 1] = 4;
	_grid[4, 4] = 5;
	_grid[14, 2] = 3;
	int r = 0;
	foreach (int v in GridValues())
	{
	    r += v;
	}
	Console.WriteLine(r);
    }
    public static IEnumerable<int> GridValues()
    {
	for (int x = 0; x < 15; x++)
	{
	    for (int y = 0; y < 15; y++)
	    {
		yield return _grid[x, y];
	    }
	}
    }
}

Output

12

GetEnumerator error. In a class, the foreach-loop is not by default supported. A GetEnumerator method (often part of the IEnumerable interface) is required.

So: We can implement IEnumerable to fix this error on a class. This provides the GetEnumerator method.

C# program that causes GetEnumerator error

using System;

class Example
{
}

class Program
{
    static void Main()
    {
	Example example = new Example();
	// This does not compile: GetEnumerator is required.
	foreach (string element in example)
	{
	    Console.WriteLine(true);
	}
    }
}

Output

error CS1579: foreach statement cannot operate on
    variables of type 'Example' because 'Example' does
    not contain a public definition for 'GetEnumerator'

Implement IEnumerable. This example implements the IEnumerable interface on an Example class. The class contains a List, and for GetEnumerator, we use the List's GetEnumerator method.

So: We forward the details of the implementation to the List. Our IEnumerable implementation relies on another.

Main: In the Main method, we call Example's constructor, which populates the _elements field.

Foreach: The foreach-loop in the Main method implicitly (secretly) calls the GetEnumerator method. So "HERE" is written.

Result: The program compiles. The foreach loop refers to the List's Enumerator and loops over the elements of the List.

C# program that implements IEnumerable

using System;
using System.Collections;
using System.Collections.Generic;

class Example : IEnumerable<string>
{
    List<string> _elements;

    public Example(string[] array)
    {
	this._elements = new List<string>(array);
    }

    IEnumerator<string> IEnumerable<string>.GetEnumerator()
    {
	Console.WriteLine("HERE");
	return this._elements.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
	return this._elements.GetEnumerator();
    }
}

class Program
{
    static void Main()
    {
	Example example = new Example(
	    new string[] { "cat", "dog", "bird" });
	// The foreach-loop calls the generic GetEnumerator method.
	// ... It then uses the List's Enumerator.
	foreach (string element in example)
	{
	    Console.WriteLine(element);
	}
    }
}

Output

HERE
cat
dog
bird

Implement, shortcut. In Visual Studio we can implement an interface with some help. Hover the mouse over the IEnumerable name after specifying it.

Then: Select from the menu one of the options. The first is probably the best choice.

Yield. This keyword is placed before "return." It is only used on enumerators. We use it in the previous example method that returns IEnumerable.

Yield

State: We can use yield to "save the state" of the function after the return. This work is performed by the compiler.

Foreach. This kind of loop has advantages. It results in simpler, clearer syntax. We no longer need to track indexes with variables (which often have names like i or x).

Foreach

Safer, more robust. With IEnumerable, our code becomes safer and more robust. It does this by hiding all the details about the underlying collection.

Extensions. Consider this. If a method needs to be implemented for every kind of List and Array (such as string lists, string arrays, int lists), our program will grow.

But: If a single extension method, one receiving IEnumerable, can be implemented instead, our code base remains much smaller.

Extension Method

Performance. A single IEnumerable method can reduce code size—this has speed advantages. For numeric methods, though, using an array directly is usually a faster approach.

A generic interface, IEnumerable provides an abstraction for looping over elements. It provides foreach-loop support. And it allows us to use LINQ extensions.


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