tags:

views:

49

answers:

3

Hi,

I've been quite surprise when I saw the metadata of ReadOnlyObservableCollection in VS 2008...

public class ReadOnlyObservableCollection<T> : ReadOnlyCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    // Summary:
    //     Initializes a new instance of the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>
    //     class that serves as a wrapper for the specified System.Collections.ObjectModel.ObservableCollection<T>.
    //
    // Parameters:
    //   list:
    //     The collection to wrap.
    public ReadOnlyObservableCollection(ObservableCollection<T> list);

    // Summary:
    //     Occurs when an item is added or removed.
    protected virtual event NotifyCollectionChangedEventHandler CollectionChanged;
    //
    // Summary:
    //     Occurs when a property value changes.
    protected virtual event PropertyChangedEventHandler PropertyChanged;

    // Summary:
    //     Raises the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>.CollectionChanged
    //     event.
    //
    // Parameters:
    //   args:
    //     The event data.
    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args);
    //
    // Summary:
    //     Raises the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>.PropertyChanged
    //     event.
    //
    // Parameters:
    //   args:
    //     The event data.
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args);
}

As you can see, CollectionChanged, a member of INotifyCollectionChanged is implemented in protected... and I can't do that in my own class.

.NET framework should not compile !

Does someone has an explanation of this mystery ?

+3  A: 

If you closely examing ReadOnlyObservableCollection you will see that it explicitly implements INotifyPropertyChanged. In other words, the protected event handler is not the real implementation of the interface - INotifyCollectionChanged.CollectionChanged is.

Mark Seemann
+1  A: 

I've checked with Reflector and here is what I found:

protected event NotifyCollectionChangedEventHandler CollectionChanged;
protected event PropertyChangedEventHandler PropertyChanged;
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged;
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;

So events actually are implemented.

Andrew Bezzub
+1  A: 

I am agree with Mark Seemann's answer and also take a look on this question some useful stuff can be found in answers. Its all due to explicit interface implementation. Try to implement INotifyPropertyChanged (or any other interface) explixetly in your type and then open it through Object Browser, you will see that is shows interface memeber even as private.

Restuta