CSharp - Extend Collection<T>

Introduction

The following code shows how to create new collection class by extending The Collection<T> class.

Demo

using System;
using System.Collections.ObjectModel;

class Animal//w ww  .  j  a va2s.  c om
{
    public string Name;
    public int Popularity;

    public Animal(string name, int popularity)
    {
        Name = name; Popularity = popularity;
    }
}

class AnimalCollection : Collection<Animal>
{
    // AnimalCollection is already a fully functioning list of animals.
    // No extra code is required.
}

class Zoo
{
    public readonly AnimalCollection Animals = new AnimalCollection();
}

class Program
{
    static void Main()
    {
        Zoo zoo = new Zoo();
        zoo.Animals.Add(new Animal("A", 10));
        zoo.Animals.Add(new Animal("B", 20));
        foreach (Animal a in zoo.Animals)
            Console.WriteLine(a.Name);
    }
}

Result