Click here to Skip to main content
13,255,961 members (48,333 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 29 Sep 2010

SmartLists - Extended Lists with Events

, 29 Sep 2010
Rate this:
Please Sign up or sign in to vote.
SmartLists library is a collection of classes that extends the standard C# List object


SmartLists library is a collection of classes that extends the standard C# List<T> object. Better: they implement the IList<T> interface, wrapping the List<T> object and adding more functionalities to it.

These lists implements solutions to common issues that usually happen when dealing with Lists. As an example, a list with a "Parent" object that is consistently updated between the elements of the list and the list itself.

These lists are not designed for performance but with easy of use and consistency in mind. If you look for performance, you better look into other kind of lists that are designed for that.


This is a list that generates events when elements are added, inserted, removed, swapped and when it is cleared. Events are generated before and after these actions, so that we can cancel the action or take further actions after the action.

Some examples of use of the events could be:

  1. To check an object before it's added to the list so that objects with inconsistent properties are not added to the list
  2. To avoid that some kind of elements are removed from the list
  3. To update GUI elements after some actions are taken on a list


This is an EventList that requires its elements to have a Name (string) property (INamed interface). In this kind of list, you can't add elements with the same name. The name comparison can be set to be case sensitive or not.

/// <summary>
/// Object that have a Name (property)
/// </summary>
public interface INamed
    /// <summary>
    /// Event fired before the Name of the object is changed
    /// Operation can be cancelled
    /// </summary>
    event CancelRenameEventHandler BeforeNameChanged;
    /// <summary>
    /// Event fired when the object has changed it's Name
    /// </summary>
    event EventHandler NameChanged;
    /// <summary>
    /// Name of the object
    /// </summary>
    string Name { get; }


This is an EventList where its elements and the list itself have a Parent object (IParented<T> interface). And we want this Parent property to be updated automatically when the object is added or removed from the list or the list's parent changes. This is really quite a common situation that happens really often and is usually coded in the most wrong and inconsistent ways.

/// <summary>
/// Objects implementing this interface have a parent object
/// </summary>
/// <typeparam name="T">Type of the parent object</typeparam>
public interface IParented<T>
    /// <summary>
    /// Event generated when the Parent property is changed
    /// </summary>
    event EventHandler ParentChanged;
    /// <summary>
    /// Get or set the Parent object
    /// </summary>
    T Parent


This is an EventList that puts together the features of the NamedList and the ParentedList. So in this kind of list, we will have items with a Name and a Parent property.

An example of use of this kind of list is a list of a database table fields. The fields have of course a Name and a Parent (the table). When a field is removed or created and added to the list, its parent is updated automatically by the list. Of course, you can't add two fields with the same name: and of course you can't rename a field with the name of another field (this generates a DuplicatedNameException). This is all handled automatically by the ParentedNamedList. The only thing the developer needs to do is create a Field object that implements the INamed and the IParent<T> interfaces.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Luigi Grilli
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMost of this exists in the BCL Pin
William E. Kempf30-Sep-10 4:15
memberWilliam E. Kempf30-Sep-10 4:15 
GeneralRe: Most of this exists in the BCL Pin
Luigi Grilli30-Sep-10 4:40
memberLuigi Grilli30-Sep-10 4:40 
Thanks William for having the time to look at this and commenting. I deeply googled for this before writing this classes, and I can say you that there are of course some solutions for this, but not as much complete as these classes.

For example, as you suggested, the ObservableCollection has a unique event (not separate event) for all the different actions. This is, in my opinion, a bad design, even if the result is the same. Anyway the Observable collections lack all the "before" events: so for example you can't cancel an action. Also it lack the "swap" method. Finally, EventList is .NET 2.0 compatible, while you need at least 3.0 for the ObservableCollection.

About the NamedList compared to KeyedCollection: yes you are right, KeyedCollection is more flexible about the key type that is generic and not a string. Anyway it lacks all the event stuff. Maybe in a future release I will have a generic key list to the library.

GeneralMy vote of 4 Pin
Michela Carriero - Lyra29-Sep-10 22:18
memberMichela Carriero - Lyra29-Sep-10 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 30 Sep 2010
Article Copyright 2010 by Luigi Grilli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid