This tutorial is about enumerable interfaces and classes.
The tutorial has two parts.
One is the theory and shows us the basic information.
The last part comes with a commented step-by-step source part that allows you to understand how to build classes for the enumerable issue.
Enumerable is a module used in the Array class that gives you Enumerator.
An Enumerator is an object that returns each item in a collection in a specific order
Both IEnumerable and IEnumerator are interfaces that implement the iterator software design pattern in the .Net
IEnumerable is a basic interface used to obtain an object that knows how to iterate or enumerate over the elements in the collection.
The IEnumerable interface actually uses IEnumerator.
IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface that iterates through a collection.
The IEnumerable method can be defined into the source code like this:
1 | public IEnumerator GetEnumerator(); |
IEnumerator has these methods:
1 2 3 | public object Current; public void Reset(); public bool MoveNext(); |
The default code to start an interface named Test_001 is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Test_001 : IEnumerable, IEnumerator { IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } public object Current { get { throw new NotImplementedException(); } } public bool MoveNext() { throw new NotImplementedException(); } public void Reset() { throw new NotImplementedException(); } } |
I create a console C# project named console_Enumerables__test_001 with Visual Studio 2019.
The next source code comes with a simple example and shows you each step to solve this issue about how to deal with classes and interfaces.
Each step from one to nine solves a programming problem.
The first step starts with an array and finishes with the value values to the class where you can continue with your own source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; namespace console_Enumerables__test_001 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); // Step 1: // let's add source code // create array years and fill with data var years = new int[] { 1976, 1976, 1999, 2020 }; // read each year and show on console foreach (var year in years) { Console.WriteLine($"Element of array year is {year}"); } // use ReadLine to read next line terminated by the Enter key. Console.ReadLine(); // Step 2: add this source code line to get info // select GetEnumerator and press F12 key // scroll on the bottom of the file and will see this // public abstract class Array : ICollection, IEnumerable, IList, IStructuralComparable, IStructuralEquatable, ICloneable // the array named years implements manye interfaces like IEnumerable and has just one method GetEnumerator years.GetEnumerator(); // Step 3: // let's use the enumerables features and change the foreach part of source code var enumerator = years.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine($"Enumerator year is {enumerator.Current}"); } // Step 7: let's use the new classes // infinite years into infinite loop array var someyears = new Infinite_YEARS_Enumerable(); foreach (var only_year in someyears) { Console.WriteLine($"Element of array only_year is {only_year}"); } // Step 8: let's use it like Step 8 but with these differences // using GetEnumerator for someyears and put on enumerator var from line 32 enumerator = someyears.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine($"Element of array only_year is {enumerator}"); } } } // Step 5: let build my custom class from IEnumerable // this way let us to create infinite years // this can be any object , I set the interface to return a type int public class Infinite_YEARS_Enumerable : IEnumerable<int> { // Step 9 : add years value to the Infinite_YEARS_Enumerable // I can use my own data in the next line commented // private int[] my_years = new int[] { 1976, 1976, 1999, 2020 }; // private int[] out_years; // create the index // private int index = 0; // replace into MoveNext() with // index++; return index < val_year.Length; // create a constructor // public MyYears(int[] val_years) //{ // out_years = val_years; //} // into next methods GetEnumerator() // change return new TheYear_Enumerator(my_years); public IEnumerator GetEnumerator() { // comment this exception //throw new NotImplementedException(); // add return with new result MyInfiniteEnumerator() return new TheYear_Enumerator(); } // because IEnumerator<int> IEnumerable<int>.GetEnumerator() { return new TheYear_Enumerator(); //throw new NotImplementedException(); } } // Step 6: let build my custom class from IEnumerator internal class TheYear_Enumerator : IEnumerator<int> { // the next line of source code is add by IDE //public object Current => throw new NotImplementedException(); public int Current { get; private set; } = 0; // the next line of source code is add by IDE //object IEnumerator.Current => throw new NotImplementedException(); // the Current method get the current position of the enumerator object IEnumerator.Current => Current; public void Dispose() { // the next line of source code is add by IDE //throw new NotImplementedException(); } public bool MoveNext() { // the next line of source code is add by IDE //throw new NotImplementedException(); // increment the position of the enumerator Current++; return true; } public void Reset() { // the next line of source code is add by IDE //throw new NotImplementedException(); // set to position 0 of the enumerator Current = 0; } } } |