Introduction
This is a C# List based object that has the same functionality as a list but as the built-in events that should have been there from the start. It has the following events:
BeforeItemAdded
ItemAdded
BeforeItemRemoved
ItemRemoved
ItemsCleared
Background
I created this GenericList
because I kept finding myself needing events like ItemAdded
or ItemRemoved
everytime I used the list. So I created this generic list.
The GenericList Inherits from the List Interface
You can see that implemented from the IList
interface. I took all the functions that the interface implements and wrapped it back to a List
inside.
public class GenericList<T> : IList<T>
{
#region Members
private List<t> m_pItems = null;
#endregion
Examples of Wrapping
#region IList Methods
public T this[int index]
{
get { return this.Items[index]; }
set { this.Items[index] = value; }
}
public int IndexOf(T item)
{
return this.Items.IndexOf(item);
}
public void Insert(int index, T item)
{
OnBeforeItemAdded(this, new GenericItemEventArgs<t>(item));
this.Items.Insert(index, item);
OnItemAdded(this, new GenericItemEventArgs<t>(item));
}
public void RemoveAt(int index)
{
OnBeforeItemRemoved(this, new GenericItemEventArgs<t>(this.Items[index]));
this.Items.RemoveAt(index);
OnItemRemoved(this, new EventArgs());
}
#endregion
I also add the functions and properties of the ICollection
interface, IEnumerable<t>
interface, and the IEnumerable
interface.
Events
The events I added are the ones I thought were most appropriate for the list.
#region Events
public event EventHandler<GenericItemEventArgs<T>> ItemAdded;
public event EventHandler<GenericItemEventArgs<T>> BeforeItemAdded;
public event EventHandler<EventArgs> ItemRemoved;
public event EventHandler<GenericItemEventArgs<T>> BeforeItemRemoved;
public event EventHandler<EventArgs> ItemsCleared;
#endregion
Event Methods
These are the functions that check whether the event is null
or not.
#region Event Methods
protected virtual void OnItemAdded(object sender, GenericItemEventArgs<T> e)
{
if (ItemAdded != null)
ItemAdded(sender, e);
}
protected virtual void OnBeforeItemAdded
(object sender, GenericItemEventArgs<T> e)
{
if (BeforeItemAdded != null)
BeforeItemAdded(sender, e);
}
protected virtual void OnItemRemoved(object sender, EventArgs e)
{
if (ItemRemoved != null)
ItemRemoved(sender, e);
}
protected virtual void OnBeforeItemRemoved
(object sender, GenericItemEventArgs<T> e)
{
if (BeforeItemRemoved != null)
BeforeItemRemoved(sender, e);
}
protected virtual void OnItemsCleared(object sender, EventArgs e)
{
if (ItemsCleared != null)
ItemsCleared(sender, e);
}
#endregion
Custom EventArgs
When creating the Events for the functions: Add
, Remove
, Insert
, and RemoveAt
I wanted a way for the developer to have access or knowledge of the item being removed or added on the events, so I created a custom EventArgs.
I created the GenericItemEventArgs
which supplies the developer to the item being added or removed for quick access.
public class GenericItemEventArgs<T> : EventArgs
{
public T Item { get; private set; }
public GenericItemEventArgs(T item)
{
this.Item = item;
}
}
Using the Code
The list is pretty simple to use. It is identical to the C# List but with events. It has the same function names and everything. If you can use the C# list, you can use this list.
Examples of code of how to use the list:
GenericList<int> List = new GenericList<int>()
List.ItemAdded += new EventHandler<Tshrove.GenericList.GenericItemEventArgs<int>>
(List_ItemAdded);
List.ItemRemoved += new EventHandler<EventArgs>(List_ItemRemoved);
List.ItemsCleared += new EventHandler<EventArgs>(List_ItemsCleared);
List.BeforeItemAdded += new EventHandler<Tshrove.GenericList.GenericItemEventArgs<int>>
(List_BeforeItemAdded);
List.BeforeItemRemoved += new EventHandler
<Tshrove.GenericList.GenericItemEventArgs<int>>(List_BeforeItemRemoved);
List.Add(5);
List.Remove(5);
List.Add(10);
List.Add(11);
List.Clear();