Action collection : Generic Collection « Generic « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

class ActionCollection<T> : Collection<T>
{
    private Action<T> action;

    public ActionCollection(Action<T> action)
    {
        this.action = action;
    }

    protected override void InsertItem(int index, T item)
    {
        action(item);
        base.InsertItem(index, item);
    }
}


public class MainClass
{
    public static void Main()
    {
        ActionCollection <string> ac = new ActionCollection<string>(Console.WriteLine);
        ac.Add("console");
    }
}
console








18.3.Generic Collection
18.3.1.Generic Collection
18.3.2.Extends Collection
18.3.3.Action collection