Skip to content

Instantly share code, notes, and snippets.

@uvbeenzaned
Last active November 5, 2017 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uvbeenzaned/4601277 to your computer and use it in GitHub Desktop.
Save uvbeenzaned/4601277 to your computer and use it in GitHub Desktop.
Can fire a registered event when an item is added to the list.
using System;
using System.Collections.Generic;
namespace NameSpace
{
public class EventList<T> : List<T>
{
public event EventHandler OnAdd;
public event EventHandler OnAddRange;
public event EventHandler OnRemove;
new public void Add(T item)
{
base.Add(item);
OnAdd?.Invoke(this, new EventListEventArgs<T>
{
AddedItem = item
});
}
new public void AddRange(IEnumerable<T> collection)
{
base.AddRange(collection);
OnAddRange?.Invoke(this, new EventListEventArgs<T>
{
AddedItems = collection
});
}
new public void Remove(T item)
{
base.Remove(item);
OnRemove?.Invoke(this, new EventListEventArgs<T>
{
RemovedItem = item
});
}
}
public class EventListEventArgs<T> : EventArgs
{
public T AddedItem { get; set; }
public IEnumerable<T> AddedItems { get; set; }
public T RemovedItem { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment