How does ObservableCollection<T>.Add work?
Asked Answered
I

3

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?

Inglenook answered 16/2, 2011 at 22:7 Comment(0)
R
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.

Romina answered 16/2, 2011 at 22:11 Comment(10)
Ah, so that's how that works, thank you once again! (How do you even know that?)Inglenook
@H.B., have you heard of Reflector?Ilarrold
@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 tirania.org/blog/archive/2011/Feb-04.htmlBarquentine
@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.Ilarrold
@Darin: All of the versions are time-bombed, so our free ride is going to come to an end soon no matter what.Romina
@Ani, yeah, that really sucks badly. Will have to crack it. Never gonna pay 35 bucks for it.Ilarrold
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.Inglenook
@H.B.: Don't waste any time on Reflector; ILSpy is the future.Thatch
@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.Frowsy
Reflector 6.8.2.5 is the version I use. Free and without time limits.Intoxicating
B
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);
}
Barquentine answered 16/2, 2011 at 22:14 Comment(1)
Thank you, too, Ani was faster though so i handed this one to him/her.Inglenook
Q
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.

Quadrat answered 16/2, 2011 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.