SoFunction
Updated on 2025-03-07

C# collection custom collection class

1. Non-generic method, inherited from CollectionBase

public class MyClass
{
    public static void Main()
    {
        StringCollection myStringCol = new StringCollection();
        ("a");
        ("b");
        (myStringCol[0]);
        foreach (string myAnimal in myStringCol)
        {
            (myAnimal);
        }
        ();
    }
}
//Custom collection classpublic class StringCollection : CollectionBase
{
    public void Add(string newAnimal)
    {
        (newAnimal);
    }

    public void Remove(string newAnimal)
    {
        (newAnimal);
    }

    public StringCollection() { }

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

2. Generic method, inherited from Collection<T>

The following code example demonstrates how to derive the collection class from the constructed type's Collection<T> generic class, and how to override the protected InsertItem, RemoveItem, ClearItem, and SetItem methods to provide custom behaviors Add, Insert, Remove, and Clear methods, and set the Item[Int32] attribute.
The custom behavior provided in this example is the notification event that is raised when each protected method ends.
The Dinosaurs class inherits Collection<string> and defines the Change event, using the DinosaursChangedEventArgs class for event information and changes using enumeration identifiers.

using System;
using ;
using ;

public class Dinosaurs : Collection<string>
{
    public event EventHandler<DinosaursChangedEventArgs> Changed;

    protected override void InsertItem(int index, string newItem)
    {
        (index, newItem);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                , newItem, null));
        }
    }

    protected override void SetItem(int index, string newItem)
    {
        string replaced = Items[index];
        (index, newItem);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                , replaced, newItem));
        }
    }

    protected override void RemoveItem(int index)
    {
        string removedItem = Items[index];
        (index);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                , removedItem, null));
        }
    }

    protected override void ClearItems()
    {
        ();

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                , null, null));
        }
    }
}

// Event argument for the Changed event.
//
public class DinosaursChangedEventArgs : EventArgs
{
    public readonly string ChangedItem;
    public readonly ChangeType ChangeType;
    public readonly string ReplacedWith;

    public DinosaursChangedEventArgs(ChangeType change, string item, 
        string replacement)
    {
        ChangeType = change;
        ChangedItem = item;
        ReplacedWith = replacement;
    }
}

public enum ChangeType
{
    Added, 
    Removed, 
    Replaced, 
    Cleared
};

public class Demo
{
    public static void Main()
    {
        Dinosaurs dinosaurs = new Dinosaurs();

         += ChangedHandler; 

        ("Psitticosaurus");
        ("Caudipteryx");
        ("Compsognathus");
        ("Muttaburrasaurus");

        Display(dinosaurs);
    
        ("\nIndexOf(\"Muttaburrasaurus\"): {0}", 
        ("Muttaburrasaurus"));

        ("\nContains(\"Caudipteryx\"): {0}", 
        ("Caudipteryx"));

        ("\nInsert(2, \"Nanotyrannus\")");
        (2, "Nanotyrannus");

        ("\ndinosaurs[2]: {0}", dinosaurs[2]);

        ("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";

        ("\nRemove(\"Microraptor\")");
        ("Microraptor");

        ("\nRemoveAt(0)");
        (0);

        Display(dinosaurs);
    }
    
    private static void Display(Collection<string> cs)
    {
        ();
        foreach( string item in cs )
        {
            (item);
        }
    }

    private static void ChangedHandler(object source, 
    DinosaursChangedEventArgs e)
    {

        if (==)
        {
            ("{0} was replaced with {1}", , 
            );
        }
        else if(==)
        {
            ("The dinosaur list was cleared.");
        }
        else
        {
            ("{0} was {1}.", , );
        }
    }
}

This is all about this article about the custom collection class of C# collections. I hope it will be helpful to everyone's learning and I hope everyone will support me more.