0

I have this list:

List<string> x=new List<string>

So, now I want to do something when the count is being increased. I tried:

if(x.Count++){
  //do stuff
}

But it did not work. So what can I try?

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • 1
    Tunnel all adding of elements trough your own delegate method which activates an event. – Jeroen Vannevel Nov 01 '13 at 21:45
  • All together the code isn't that extensive; it sounds more than it is. You have to utilize event handlers that aren't directly available so you have to work around that. – Jeroen Vannevel Nov 01 '13 at 21:46

1 Answers1

5

You can't do this like you're trying to do. if (x.Count++) makes no sense - you're attempting to increment the count (which is read-only).

I would derive from List<T> and add ItemAdded and ItemRemoved events.

Actually, that would be re-inventing the wheel. Such a collection already exists. See ObservableCollection<T>, which raises a CollectionChanged event. The NotifyCollectionChangedEventArgs tells you what changed.

Example (not tested):

void ChangeHandler(object sender, NotifyCollectionChangedEventArgs e ) {
    switch (e.Action) {
        case NotifyCollectionChangedAction.Add:
            // One or more items were added to the collection.
            break;
        case NotifyCollectionChangedAction.Move:
            // One or more items were moved within the collection.
            break;
        case NotifyCollectionChangedAction.Remove:
            // One or more items were removed from the collection.
            break;
        case NotifyCollectionChangedAction.Replace:
            // One or more items were replaced in the collection.
            break;
        case NotifyCollectionChangedAction.Reset:
            // The content of the collection changed dramatically.
            break;
    }

    // The other properties of e tell you where in the list
    // the change took place, and what was affected.
}

void test() {
    var myList = ObservableCollection<int>();
    myList.CollectionChanged += ChangeHandler;

    myList.Add(4);
}

References:

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Looks like the solution. You'll have to check the initial size and the current size of the list to find out if something has been added or removed; the appropriate event doesn't seem to differentiate between the two. – Jeroen Vannevel Nov 01 '13 at 21:49
  • The `List.Add()` method and other relevant methods are non-virtual so you can't really make that work. You would have to reimplement list to do that. – Jeff Mercado Nov 01 '13 at 21:49
  • 1
    +1 Or `BindingList` which has events for every type of mutation. It has also the [`AddingNew`](http://msdn.microsoft.com/en-us/library/vstudio/ms132741.aspx) event which is raised _before_ an item is added. – Tim Schmelter Nov 01 '13 at 21:52
  • @TimSchmelter Thanks for the alternative solution (which of course is better for binding). However `"has events for every type of mutation"` doesn't seem accurate - it has essentially the same, single [`ListChanged` event](http://msdn.microsoft.com/en-us/library/ms132740(v=vs.110).aspx). – Jonathon Reinhart Nov 01 '13 at 22:01