Evented List : List « Collections Data Structure « C# / C Sharp






Evented List

        

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

namespace System.Collections.Generic
{
  public class ItemAtEventArgs<T> : EventArgs
  {
    public readonly T Item;
    public readonly int Index;
    public ItemAtEventArgs(T item, int index) { Item = item; Index = index; }
    public override string ToString()
    {
      return String.Format("(ItemAtEventArgs {0} '{1}')", Index, Item);
    }
  }

  public class ItemRangeEventArgs<T> : EventArgs
  {
    public readonly T[] Items;
    public readonly int Index, Count;
    public ItemRangeEventArgs(T[] items, int index, int count) { Items = items; Index = index; Count = count; }
    public override string ToString()
    {
      return String.Format("(ItemRangeEventArgs {0} Cnt: {2})", Index, Items, Count);
    }
  }

  public class EventedList<T> : List<T>
  {
    public EventedList() : base() { }
    public EventedList(int capacity) : base(capacity) { }
    public EventedList(IEnumerable<T> collection) : base(collection) { }

    public event ItemInsertedHandler ItemInserted;
    public event ItemRemovedHandler ItemRemoved;
    public event ItemRangeInsertedHandler ItemRangeInserted;
    public event ItemRangeRemovedHandler ItemRangeRemoved;

    public delegate void ItemInsertedHandler(object sender, ItemAtEventArgs<T> e);
    public delegate void ItemRemovedHandler(object sender, ItemAtEventArgs<T> e);
    public delegate void ItemRangeInsertedHandler(object sender, ItemRangeEventArgs<T> e);
    public delegate void ItemRangeRemovedHandler(object sender, ItemRangeEventArgs<T> e);

    protected virtual void OnItemRemoved(ItemAtEventArgs<T> e)
    {
      ItemRemovedHandler handler1 = this.ItemRemoved;
      if (handler1 != null)
        handler1(this, e);
    }

    protected virtual void OnItemInserted(ItemAtEventArgs<T> e)
    {
      ItemInsertedHandler handler1 = this.ItemInserted;
      if (handler1 != null)
        handler1(this, e);
    }

    protected virtual void OnItemRangeRemoved(ItemRangeEventArgs<T> e)
    {
      ItemRangeRemovedHandler handler1 = this.ItemRangeRemoved;
      if (handler1 != null)
        handler1(this, e);
    }

    protected virtual void OnItemRangeInserted(ItemRangeEventArgs<T> e)
    {
      ItemRangeInsertedHandler handler1 = this.ItemRangeInserted;
      if (handler1 != null)
        handler1(this, e);
    }

    public new void Add(T item)
    {
      base.Add(item);
      OnItemInserted(new ItemAtEventArgs<T>(item, base.Count - 1));
    }

    public new void AddRange(IEnumerable<T> collection)
    {
      this.InsertRange(base.Count, collection);
    }

    public new void Insert(int index, T item)
    {
      base.Insert(index, item);
      OnItemInserted(new ItemAtEventArgs<T>(item, index));
    }

    public new void InsertRange(int index, IEnumerable<T> collection)
    {
      int count = base.Count;
      base.InsertRange(index, collection);
      count = base.Count - count;
      OnItemRangeInserted(new ItemRangeEventArgs<T>(base.GetRange(index, count).ToArray(), index, count));
    }

    public new bool Remove(T item)
    {
      int num1 = base.IndexOf(item);
      if (num1 >= 0)
      {
        this.RemoveAt(num1);
        return true;
      }
      return false;
    }

    public new void RemoveAt(int index)
    {
      T item = base[index];
      base.RemoveAt(index);
      OnItemRemoved(new ItemAtEventArgs<T>(item, index));
    }

    public new void RemoveRange(int index, int count)
    {
      List<T> rc = base.GetRange(index, count);
      base.RemoveRange(index, count);
      OnItemRangeRemoved(new ItemRangeEventArgs<T>(rc.ToArray(), index, count));
    }
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.new List() Add(assembly1) Indexer
2.Get the size of a list
3.Add Item to a list object
4.Search Test
5.Remove Item from a List
6.Change Contents
7.Reverse Contents
8.Hash List to Hash Array
9.Load elements into a generic list from an array
10.Lazy List
11.Slice list
12.Chunked List
13.Flatten a List
14.Keyed List
15.Convert a collection of strings to a comma separated list.
16.Convert List To Array
17.Compare two Lists
18.Name Value Pair List
19.Get Distinct value from a List
20.Adds the elements of the specified collection to the specified generic IList.
21.List(T) Class represents a strongly typed list of objects that can be accessed by index.
22.IList Interface represents a non-generic collection of objects that can be individually accessed by index.
23.Searches a range of elements in the sorted List for an element using the specified comparer and returns the zero-based index of the element.
24.Determines whether an element is in the List.
25.Converts the elements in the current List to another type, and returns a list containing the converted elements.
26.Copies a range of elements from the List to a compatible one-dimensional array, starting at the specified index of the target array.
27.Determines whether the List contains elements that match the conditions defined by the specified predicate.
28.Performs the specified action on each element of the List.
29.Searches for object and returns the zero-based index
30.Searches for object and returns the zero-based index of the last occurrence
31.Reverses the order of the elements in the specified range.
32.Weak Collection
33.Sorted Collection
34.Paged List
35.Get Search List
36.Find All Index
37.SortedList
38.Reverse a List
39.Shuffles the specified list
40.Sequence is an abstraction of a data reseqeuncer
41.Generic Paged List
42.A list implementation that is loaded the first the contents are examined
43.Creates a list by combining two other lists into one.
44.Creates a list by repeating another list.
45.Compare two arrays and two lists