views:

97

answers:

5
class Foo(){
  public List<string> SomeCollection;
}

I need to implement an event which can fires when something added or removed from the Collection. How to do this?

A: 

You might take a look at this tutorial on making your own custom events.

Lucas McCoy
A: 

You can do this by using an ObservableCollection instead of a List.

Reed Copsey
+2  A: 

Take a look at the BindingList and ObservableCollection classes (in the System.ComponentModel and System.Collections.ObjectModel namespaces respectively) - either one should do the job well for you.

Note that the two classes generally provide the same functionality, but they do differ slightly. BindingList is typically more suitable for data-binding/UI purposes (hence it's name), since it allows the option to cancel updates and such. However, ObservableCollection is possibly more appropiate in your case, since you're just interested in being notified of changes (it would seem), and the class was designed purely from that perspective. The fact that they exist in very different namespaces sort of hints at this. If you want the precise details on the similarities and differences, I recommend you inspect the linked MSDN docs.

Noldorin
(replied to comment; also - isn't ObservableCollection<T> in 3.0? Which can be a factor...)
Marc Gravell
MSDN seems to suggest that ObservableCollection has been around since .NET 2.0. In terms of functionality, you are basically correct in saying that BindingList offers everything ObservableCollection does (and a bit more). One noticable point is however that ObservableCollection implements INotifyCollectionChanged and INotifyPropertyChanged, which are useful in some cases. I also believe WPF requires ObservableCollection in a few cases. As I point out, the intended usage is also slightly different, though this is a petty matter.
Noldorin
+2  A: 

List<T> has no notification support. You could look at BindingList<T>, which has events - or Collection<T>, which can be inherited with override methods.

If you want to expose the event at the Foo level, perhaps something like below - but it may be easier to leave it on the list:

class Foo{
    public event EventHandler ListChanged;
    private readonly BindingList<string> list;
    public Foo() {
        list = new BindingList<string>();
        list.ListChanged += list_ListChanged;
    }
    void  list_ListChanged(object sender, ListChangedEventArgs e) {
        EventHandler handler = ListChanged;
        if (handler != null) handler(this, EventArgs.Empty);
    }
    public IList<string> SomeCollection {get {return list;}}
}
Marc Gravell
I don't see the point of creating a custom Collection class when ObservableCollection does the job perfectly well.
Noldorin
@Noldorin - BindingList<T> is equally sufficient. Re subclassing Collection<T>; there are occasions where this is suitable - it depends on the scenario.
Marc Gravell
@Marc: Indeed, BindingList does the job fine; I wasn't debating that. And as you say, deriving from Collection<T> can sometimes be appropiate, though it would perhaps make more sense to use ObservableCollection<T> directly here, or even subclass it.
Noldorin
What would ObservableCollection<T> that BindingList<T> doesn't? Out of genuine curiosity.
Marc Gravell
A: 

basic one...

here is a good link

public class Foo
{
private List<string> _SomeCollection;
public event EventHandler Added;

public void Add(string item)
{
    SomCollection.Add(item);
    OnAdd();

}

private void OnAdd()
{
    if (Added != null)
    {
        Added.Invoke(this, EventArgs.Empty);
    }
}
}
J.13.L
I guess it depends on what class you want the event to be fire from...
J.13.L