patterncsharpMinor
EventList<T> a generic list implementation that raises events when items are added and removed
Viewed 0 times
genericimplementationeventsremovedareaddedanditemsthateventlist
Problem
I created this class for a project when I wanted to have
I looked at using
```
public class EventList : IList
{
private readonly List _list;
public EventList()
{
_list = new List();
}
public EventList(IEnumerable collection)
{
_list = new List(collection);
}
public EventList(int capacity)
{
_list = new List(capacity);
}
public event EventHandler> ItemAdded;
public event EventHandler> ItemRemoved;
private void RaiseEvent(EventHandler> eventHandler, T item, int index)
{
var eh = eventHandler;
eh?.Invoke(this, new EventListArgs(item, index));
}
public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
var index = _list.Count;
_list.Add(item);
RaiseEvent(ItemAdded, item, index);
}
public void Clear()
{
for (var index = 0; index _list.Count;
public bool IsReadOnly => false;
public int IndexOf(T item)
{
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
_list.Insert(index, item);
RaiseEvent(ItemRemoved, item, index);
}
public void RemoveAt(int index)
{
var item = _list[index];
_list.RemoveAt(index);
RaiseEvent(ItemRemoved, item, index);
}
public T this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
}
public class EventListArgs : EventArgs
{
public EventListArgs(T item, int index)
{
Item = item;
Index = index;
}
public T Item { ge
List properties on a class and also listen for items being added and removed. I looked at using
BindingList or ObservableCollection but I felt that their collection changed events are too complicated. I simply wanted to know when items are added and removed.```
public class EventList : IList
{
private readonly List _list;
public EventList()
{
_list = new List();
}
public EventList(IEnumerable collection)
{
_list = new List(collection);
}
public EventList(int capacity)
{
_list = new List(capacity);
}
public event EventHandler> ItemAdded;
public event EventHandler> ItemRemoved;
private void RaiseEvent(EventHandler> eventHandler, T item, int index)
{
var eh = eventHandler;
eh?.Invoke(this, new EventListArgs(item, index));
}
public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
var index = _list.Count;
_list.Add(item);
RaiseEvent(ItemAdded, item, index);
}
public void Clear()
{
for (var index = 0; index _list.Count;
public bool IsReadOnly => false;
public int IndexOf(T item)
{
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
_list.Insert(index, item);
RaiseEvent(ItemRemoved, item, index);
}
public void RemoveAt(int index)
{
var item = _list[index];
_list.RemoveAt(index);
RaiseEvent(ItemRemoved, item, index);
}
public T this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
}
public class EventListArgs : EventArgs
{
public EventListArgs(T item, int index)
{
Item = item;
Index = index;
}
public T Item { ge
Solution
Bug
I don't think that you want to raise an
In general I like your implementation. It is leightweight and is doing exactly what it should.
But for sure I have something to critizise:
-
The name
-
You implement
-
The
See @RomanReiner's answer here
public void Insert(int index, T item)
{
_list.Insert(index, item);
RaiseEvent(ItemRemoved, item, index);
}I don't think that you want to raise an
ItemRemoved event in the case of inserting an item. In general I like your implementation. It is leightweight and is doing exactly what it should.
But for sure I have something to critizise:
-
The name
EventList which reads like a list of events instead of a list with events. Because there is already a ObservableCollection maybe an ObservableList would be a better name. -
You implement
IList but have the underlaying list as a List instead of an IList like private readonly IList _list;. -
The
RemoveAt() could be simplified by calling the Remove() method like so public void RemoveAt(int index)
{
Remove(_list[index]);
}See @RomanReiner's answer here
- IMO the raising of multiple
ItemRemovedevents inside theClear()method isn't that good because you are raising the event before the item is removed. You should either have an additional eventItemsClearedor you should raise theItemRemovedevents after the clearing of that list.
Code Snippets
public void Insert(int index, T item)
{
_list.Insert(index, item);
RaiseEvent(ItemRemoved, item, index);
}public void RemoveAt(int index)
{
Remove(_list[index]);
}Context
StackExchange Code Review Q#111358, answer score: 5
Revisions (0)
No revisions yet.