7

I was trying to implement a specialized collection that works like ObservableCollection to encapsulate some more mechanisms in it, to do that i also let my collection inherit from Collection and i also implement the same interfaces.

I just do not get though how one actually implements the whole collection-changed-logic, for example Collection<T>.Add is not being overridden (it is not even marked as virtual), so how does the ObservableCollection fire the CollectionChanged event if items were added using that method?

H.B.
  • 136,471
  • 27
  • 285
  • 357

3 Answers3

10

To answer your specific question, Collection<T>.Add calls the InsertItem virtual method (after checking that the collection is not read-only). ObservableCollection<T> indeed overrides this method to do the insert and raise the relevant change notifications.

Ani
  • 103,292
  • 21
  • 241
  • 294
  • Ah, so that's how that works, thank you once again! (How do you even know that?) – H.B. Feb 16 '11 at 22:14
  • 1
    @H.B., have you heard of [Reflector](http://www.red-gate.com/products/dotnet-development/reflector/)? – Darin Dimitrov Feb 16 '11 at 22:16
  • @Darin I have but they told me it costs money now and they will hunt me down if I leave a copy on my system http://tirania.org/blog/archive/2011/Feb-04.html – Aaron McIver Feb 16 '11 at 22:18
  • @H.B., nobody forces you to use V7. But I must say that it's a crappy decision from RedGate to make the best .NET tool ever being paid. – Darin Dimitrov Feb 16 '11 at 22:19
  • @Darin: All of the versions are time-bombed, so our free ride is going to come to an end soon no matter what. – Ani Feb 16 '11 at 22:20
  • @Ani, yeah, that really sucks badly. Will have to crack it. Never gonna pay 35 bucks for it. – Darin Dimitrov Feb 16 '11 at 22:21
  • I heard of reflector but i was quite unaware of its uses and i never used it myself so far. Thank you for the link, maybe i shall give the free version a spin some time. – H.B. Feb 16 '11 at 22:22
  • 1
    @H.B.: Don't waste any time on Reflector; ILSpy is the future. – Rick Sladkey Feb 16 '11 at 22:36
  • 10
    @Darin Dimitrov *"Best .NET tool"* *"Will have to crack it. Never gonna pay 35 bucks for it."* ...Your concept of value is completely baffling to me. – Dan J Feb 16 '11 at 23:01
  • Reflector 6.8.2.5 is the version I use. Free and without time limits. – AMissico Feb 06 '12 at 17:18
8

It does so by calling InsertItem which is overridden and can be seen upon decompilation

protected override void InsertItem(int index, T item)
{
    this.CheckReentrancy();
    base.InsertItem(index, item);
    this.OnPropertyChanged("Count");
    this.OnPropertyChanged("Item[]");
    this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}
Aaron McIver
  • 23,797
  • 5
  • 53
  • 83
0

Remember, the key is not in overriding the base Collection methods, it's in the fact that you will be implementing the ICollection interface. And frankly, rather than inheriting from a Collection class, I would suggest instead creating an adapter class that takes a ICollection in the constructor and your methods will just delegate to the inner collection and raise the appropriate events.

Rich
  • 2,078
  • 1
  • 15
  • 16