C# gives you the ability to create your own collection classes. For example, you can create a class that can contain multiple instances of another class. This class will have the features such as adding and removing new instances from its collection. The following program shows an example.

using System.Collections;
 
namespace CreatingYourOwnCollection
{
    public class Animal
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public double Height { get; set; }
 
        public Animal(string name, int age, double height)
        {
            Name = name;
            Age = age;
            Height = height;
        }
    }
 
    public class Animals : CollectionBase           
    {
        public void Add(Animal newAnimalnewAnimal)
        {
            List.Add(newAnimalnewAnimal);
        }
 
        public void Remove(Animal oldAnimal)
        {
            List.Remove(oldAnimal);
        }
    }
}

Example 1 – Creating Your Own Collection

The above code declares two classes. First is the Animal class   (lines 5-17) which will represent an element in our collections class. The second is our collection class   named Animals (lines 19-30) which will contain a collection of Animal objects. To benefit with the functionality of collections, our class inherits the CollectionBase class.

We need to import the System.Collections namespace. The   CollectionBase class has methods such as Add() and Remove() and a List property that will contain the collection of objects. If we want to add or remove objects, we simply use the List property and the corresponding methods. The methods of the List property accepts an object and not an Animal for example. So when defining an indexer, we need to cast the result first before returning it to the user.

public Animal this[int index]
{
   get { return (Animal)List[index]; }
   set { List[index] = value; }
}

The code above is added to our collection class so we can access each object in the collection through their index positions. Again, we used the List property to access the objects and by using the specified index. Since the List contains object elements, we need to cast them to Animal objects first. Let’s try to create a program that uses the classes we have just created.

namespace CreatingYourOwnCollection
{
    public class Program
    {
        public static void Main()
        {
            Animals animalCollection = new Animals();
 
            animalCollection.Add(new Animal("Jack", 10, 100));
            animalCollection.Add(new Animal("Sussy", 5, 10)); 
            animalCollection.Add(new Animal("Frank", 3, 5));  
 
            for (int i = 0; i < animalCollection.Count; i++)
            {
                Console.WriteLine("Animal {0}", i + 1);
                Console.WriteLine("Name: {0}", animalCollection[i].Name);
                Console.WriteLine("Age: {0}", animalCollection[i].Age);
                Console.WriteLine("Height: {0}
", animalCollection[i].Height);
            }
        }
    }
}

Example 2 – Custom Collection in Action

Animal 1
Name: Jack
Age: 10
Height: 100

Animal 2
Name: Sussy
Age: 5
Height: 10

Animal 3
Name: Frank
Age: 3
Height: 5

We created a new instance of the Animals collection class then added three instances to it using the Add method. We then iterate through each element using a for loop to show that we can access them by their indexes. You can also use a foreach statement to loop for each animal.

foreach (Animal animal in animalCollection)
{

}

Once you learn generics, there is an easier way of creating a collection of any types without creating a class that inherits from CollectionBase.