Custom Collection Classes In C#

Introduction

Being  .NET developers, we all are familiar with collections and generics. We know ArrayLists, Arrays, List and Dictionary etc. as collection classes through which we can iterate. This article will explain  how one can create our own custom collection class, which can be iterated through. The article follows a step by step process to create a custom collection class to know what ait ctually takes to create a collection class. The purpose of the article is to learn the concept of collection classes through step by step practical implementations.

C#

Prerequisites

The prerequisite to learn how to create a custom class is no more than knowing how to code in C#. This article uses a console Application named CollectionClassConcept, which contains an empty class named CollectionClass. We’ll use this initial setup to learn about collection classes.

C#

Step 1

Considering this, we already have a collection class. It does not matter, if it is empty for now. Try to access the CollectionClass in Main method of Program.cs, create an instance of the class and iterate through it.

CollectionClass 

  1. namespace CollectionClassConcept  
  2. {  
  3.   public class CollectionClass  
  4.   {  
  5.   
  6.   }  
  7. }   

Program.cs 

  1. using System;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   class Program  
  6.   {  
  7.     static void Main(string[] args)  
  8.     {  
  9.       CollectionClass collectionClass=new CollectionClass();  
  10.       foreach (string  collection in collectionClass)  
  11.       {  
  12.         Console.WriteLine(collection);  
  13.       }  
  14.     }  
  15.   }  
  16. }   

When the solution is compiled, it ends up having a compile time error, as shown below.

C#
Error

foreach statement cannot operate on variables of type 'CollectionClassConcept.CollectionClass' because 'CollectionClassConcept.CollectionClass' does not contain a public definition for 'GetEnumerator'

Step 2

As the error suggests that a class needs to have GetEnumerator method for it to be iterative. Thus, add a new GetEnumerator() method into the CollectionClass class.

CollectionClass 

  1. namespace CollectionClassConcept  
  2. {  
  3.   public class CollectionClass  
  4.   {  
  5.     public int GetEnumerator()  
  6.     {  
  7.         
  8.     }  
  9.   }  
  10. }   

Program.cs 

  1. using System;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   class Program  
  6.   {  
  7.     static void Main(string[] args)  
  8.     {  
  9.       CollectionClass collectionClass=new CollectionClass();  
  10.       foreach (string  collection in collectionClass)  
  11.       {  
  12.         Console.WriteLine(collection);  
  13.       }  
  14.     }  
  15.   }  
  16. }   

When the solution is compiled, it ends up having a compile time error, as shown below.

C#

Error

  • CS0161 'CollectionClass.GetEnumerator()': not all code paths return a value
  • CS0117 'int' does not contain a definition for 'Current'
  • CS0202 foreach requires that the return type 'int' of 'CollectionClass.GetEnumerator()' must have a suitable public MoveNext method and public Current property

Now, in the code given above, foreach here makes an attempt to execute the GetEnumertaor method, but fails because it expects a MoveNext method and a property named Current. In addition to it, also expect the method to return any value other than integer.

Step 3

At this point, one can create a new class named CollectionEnumerator implementing the interface named IEnumerator and return the instance of this class from GetEnumerator method of CollectionClass.

CollectionEnumerator 

  1. using System.Collections;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   public class CollectionEnumerator : IEnumerator  
  6.   {  
  7.   }  
  8. }   

CollectionClass 

  1. using System.Collections;  
  2. namespace CollectionClassConcept  
  3. {  
  4.   public class CollectionClass  
  5.   {  
  6.     public IEnumerator GetEnumerator()  
  7.     {  
  8.       return new CollectionEnumerator();  
  9.     }  
  10.   }  
  11. }   

Program.cs 

  1. using System;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   class Program  
  6.   {  
  7.     static void Main(string[] args)  
  8.     {  
  9.       CollectionClass collectionClass=new CollectionClass();  
  10.       foreach (string  collection in collectionClass)  
  11.       {  
  12.         Console.WriteLine(collection);  
  13.       }  
  14.     }  
  15.   }  
  16. }  

C#

Error

  • 'CollectionEnumerator' does not implement an interface member 'IEnumerator.Current'.
  • 'CollectionEnumerator' does not implement an interface member 'IEnumerator.MoveNext()'.
  • 'CollectionEnumerator' does not implement an interface member 'IEnumerator.Reset()'.

Thus, it is very clear that the IEnumerator interface has the class CollectionEnumerator, which has three methods and are required to be implemented in the child class.

Step 4

Let’s try to implement these methods in the CollectionEnumerator class and see the results.

CollectionEnumerator 

  1. using System.Collections;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   public class CollectionEnumerator : IEnumerator  
  6.   {  
  7.     public bool MoveNext()  
  8.     {  
  9.       System.Console.WriteLine("Inside MoveNext Method");  
  10.       return true;  
  11.     }  
  12.   
  13.     public void Reset()  
  14.     {  
  15.       System.Console.WriteLine("Inside Reset Method");  
  16.     }  
  17.   
  18.     public object Current  
  19.     {  
  20.       get  
  21.       {  
  22.         System.Console.WriteLine("Inside Current Property");  
  23.         return "Current Property";  
  24.       }  
  25.     }  
  26.   }  
  27. }   

CollectionClass 

  1. using System.Collections;  
  2. namespace CollectionClassConcept  
  3. {  
  4.   public class CollectionClass  
  5.   {  
  6.     public IEnumerator GetEnumerator()  
  7.     {  
  8.       return new CollectionEnumerator();  
  9.     }  
  10.   }  
  11. }   

Program.cs 

  1. using System;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   class Program  
  6.   {  
  7.     static void Main(string[] args)  
  8.     {  
  9.       CollectionClass collectionClass=new CollectionClass();  
  10.       foreach (string  collection in collectionClass)  
  11.       {  
  12.         Console.WriteLine(collection);  
  13.       }  
  14.     }  
  15.   }  
  16. }   

When we run the Application, we see no compile or run time error, but the console window shows endless lines repeating them in a loop, as shown below.

C#

What causes this endless output? Let’s try to find out. From the CollectionClass class, GetEnumerator method is called from the foreach loop. Here it is expecting this GetEnumerator method to return something that is IEnumerator so that iteration or enumeration can happen. Thereafter it invokes the MoveNext method from the returned instance of CollectionEnumerator class, so in case MoveNext is returning a true value, that means implicitly that there is some data that could be read, and in turn it calls the Current property to get that data. When Current property is called, it writes “Inside Current Property” and the get accessor of the property always returns “Current Property” text because we mentioned that in the code. Now again MoveNext is called and as per our defined case, again true is returned, so it happens to be an endless loop now. In case MoveNext returns false stating that no more data is left, the loop will stop there. Let’s try to do this in next step.

Step 5

CollectionEnumerator 

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace CollectionClassConcept  
  6. {  
  7.   public class CollectionEnumerator : IEnumerator  
  8.   {  
  9.     public List<string> StringList=new List<string>(4) { "Value One""Value Two""Value Three""Value Four""Value Five" };  
  10.     public int Counter = -1;  
  11.   
  12.     public bool MoveNext()  
  13.     {  
  14.       Counter++;  
  15.       Console.WriteLine("Inside MoveNext Method : " + Counter);  
  16.       return Counter != 5;  
  17.     }  
  18.   
  19.     public void Reset()  
  20.     {  
  21.       Console.WriteLine("Inside Reset Method");  
  22.     }  
  23.   
  24.     public object Current  
  25.     {  
  26.       get  
  27.       {  
  28.         Console.WriteLine("Inside Current Property : " + StringList[Counter]);  
  29.         return StringList[Counter];  
  30.       }  
  31.     }  
  32.   }  
  33. }   

CollectionClass 

  1. using System.Collections;  
  2. namespace CollectionClassConcept  
  3. {  
  4.   public class CollectionClass  
  5.   {  
  6.     public IEnumerator GetEnumerator()  
  7.     {  
  8.       return new CollectionEnumerator();  
  9.     }  
  10.   }  
  11. }   

Program.cs 

  1. using System;  
  2.   
  3. namespace CollectionClassConcept  
  4. {  
  5.   class Program  
  6.   {  
  7.     static void Main(string[] args)  
  8.     {  
  9.       CollectionClass collectionClass=new CollectionClass();  
  10.       foreach (string  collection in collectionClass)  
  11.       {  
  12.         Console.WriteLine(collection);  
  13.       }  
  14.     }  
  15.   }  
  16. }   

Output

C#

In the CollectionEnumerator class, a generic List of strings is created. One can also create an array or ArrayList. In this case, we are using List having 5 members with values: "Value One", "Value Two", "Value Three", "Value Four", and “Value Five". There is a counter variable initialized to -1. Every time MoveNext method is called, the counter value is incremented by 1, now we specifically specified the length of the list as 5 and in the MoveNext method we specify to return false if the counter exceeds 5. Therefore the counter will have a track on how many times the MoveNext method is called. Whenever MoveNext method returns true, the Current property gets called that returns the string member from the defined list i.e. StringList indexed at the current value of the counter. In the similar way one can iterate through the list based on the length of the list or array.

Step 6

Let’s now try making the collection class independent of fixed length for e.g. we used 5 in the previous example. So we’ll do the implementation to dynamically take the input and iterate through it via calculating its length and accordingly process and show the output.

CollectionEnumerator 

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5.   
  6. namespace CollectionClassConcept  
  7. {  
  8.   public class CollectionEnumerator : IEnumerator  
  9.   {  
  10.     public List<string> StringList;  
  11.     public int Counter = -1;  
  12.   
  13.     public CollectionEnumerator(string parameter)  
  14.     {  
  15.       StringList = parameter.Split(' ').ToList();  
  16.     }  
  17.   
  18.     public bool MoveNext()  
  19.     {  
  20.       Counter++;  
  21.       Console.WriteLine("Inside MoveNext Method : " + Counter);  
  22.       return Counter != StringList.Count;  
  23.     }  
  24.   
  25.     public void Reset()  
  26.     {  
  27.       Console.WriteLine("Inside Reset Method");  
  28.     }  
  29.   
  30.     public object Current  
  31.     {  
  32.       get  
  33.       {  
  34.         Console.WriteLine("Inside Current Property : " + StringList[Counter]);  
  35.         return StringList[Counter];  
  36.       }  
  37.     }  
  38.   }  
  39. }   

CollectionClass 

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace CollectionClassConcept  
  5. {  
  6.   public class CollectionClass  
  7.   {  
  8.     private string _parameter;  
  9.   
  10.     public CollectionClass(string parameter)  
  11.     {  
  12.       _parameter = parameter;  
  13.     }  
  14.     public IEnumerator GetEnumerator()  
  15.     {  
  16.       return new CollectionEnumerator(_parameter);  
  17.     }  
  18.   }  
  19. }   

Program.cs 

  1. using System;  
  2. using System.Security.AccessControl;  
  3.   
  4. namespace CollectionClassConcept  
  5. {  
  6.   class Program  
  7.   {  
  8.     static void Main(string[] args)  
  9.     {  
  10.       CollectionClass collectionClass=new CollectionClass("We know what is a collection class.");  
  11.       foreach (string  collection in collectionClass)  
  12.       {  
  13.         Console.WriteLine(collection);  
  14.       }  
  15.       Console.ReadLine();  
  16.     }  
  17.   }  
  18. }   

The explanation of the above written code is very straight forward and self-explanatory. In the main method of Program class, when we try to create an instance of CollectionClass we pass string as a parameter keeping in mind that the class has a parameterized constructor that takes one string argument. So CollectionClass constructor being called first holds the parameter in variable _parameter. Now, the foreach statement invokes GetEnumerator which in turn creates an instance of CollectionEnumerator class and pass _parameter as a parameter to the constructor of CollectionEnumerator keeping in mind that CollectionEnumerator class now also has a parameterized constructor taking one string argument. As per the implementation of CollectionEnumerator class, as soon as its constructor is called, the parameter is split by a space character via Split method of strings. One can also supply more split character options by providing an array of splitters to the split method here, for now we’ll use space as a splitter. We convert the array that we got after split to a List of string and initialize that to StringList list defined in the class. Now to make the enumeration work independently of the fixed length, we’ll not use a constant number for the length but the length of the list that we have got in the constructor. So in the MoveNext method it returns false only if the counter does not match the count of the list. So at the end we have a collection class that is custom as per our implementation and iterates through the string on the words inside. One can also customize it further to match the requirement.

Conclusion

This article covered the topic of Collection Class/ Collection Object in detail. Creating a collection class may sound a bit tough but it’s not that tough, and it helps us to have full control over the kind of enumeration we want to use. Happy Coding.

Source Code on Github

Source Code

Other Series

My series of articles

Read more

For more technical articles, you can reach out to CodeTeddy.


Similar Articles