runtime icon indicating copy to clipboard operation
runtime copied to clipboard

ReadOnlyObservableCollection.CollectionChanged should be made public

Open davkean opened this issue 11 years ago • 15 comments

The ReadOnlyObservableCollection.CollectionChanged event is protected, which is flawed, it prevents you from being able to observe changes to it without casting to INotifyCollectionChanged.

I suspect the original thinking was because the collection is "read-only" it never changes - this is not correct. Read-only in .NET means that the consumer has a read-only view over a collection (which is why we project IReadOnlyList<T> to IVectorView<T>), not that the contents are immutable and never change.

While we've made protected members public in the past (such as Exception.HResult), this event was marked virtual, which while binary compatible, would be a source-breaking change for those that overrode it. I suspect very little have overridden it, but we may want to consider adding a new event with a different name such as Changed.

davkean avatar Feb 20 '15 06:02 davkean

I believe the original thinking was the NotifyCollectionChangedEventArgs structure could expose the underlying (non-readonly) list in certain scenarios

That was the behavior on Compact Framework, but not the behavior on full CLR. Given that CF implementation of this particular class came after the desktop implementation, I don't believe this is the history.

davkean avatar Feb 21 '15 02:02 davkean

I agree that it's unlikely users would override this event. The primary reason to override it would be to address the specific accessibility problem you describe here, but that isn't allowed in C#. A more likely scenario would be implementing a new publicly-exposed event which simply attaches listeners to the event in the base class. The latter use case would not break if the accessibility of the event was changed.

sharwell avatar Feb 21 '15 04:02 sharwell

@nguerrera @terrajobst, thoughts?

davkean avatar Feb 21 '15 04:02 davkean

I agree with this proposal. We'd have to change desktop and source-breaking can mean app-breaking in ASP.NET, correct? I think the risk is very low, but it would depend on what we're willing to accept in the desktop framework I suppose. It would be unfortunate to clutter things with a property name that doesn't match the interface. cc @ericstj for compat concern.

nguerrera avatar Feb 24 '15 21:02 nguerrera

+1 Please do this! As it stands there is no difference between ReadOnlyObservableCollection and ReadOnlyCollection unless you cast which is not API-friendly. The sole reason for the existence of ReadOnlyObservableCollection is to add events to ReadOnlyCollection, so why shy away from that?

jnm2 avatar Apr 16 '15 17:04 jnm2

See my comment on dotnet/corefx#1029. This is actually binary breaking.

nguerrera avatar Aug 19 '15 04:08 nguerrera

Can we add an event under a new name, such as just Changed? We could even go the extra mile and make the event args strongly typed i.e. provide the values as IList<T> rather than the non-generic IList.

terrajobst avatar Sep 15 '15 18:09 terrajobst

Anyone willing to write up a formal api proposal ?

danmoseley avatar Dec 16 '16 23:12 danmoseley

Any updates on this issue?

AtosNicoS avatar Apr 03 '19 05:04 AtosNicoS

We would welcome an API proposal towards addressing this issue.

eiriktsarpalis avatar Mar 25 '20 16:03 eiriktsarpalis

Would the API proposal be toward making the event public, with the various compat implications people have raised above? Or would this be a Changed event similar to the suggestion by @terrajobst?

Marv51 avatar Mar 30 '20 09:03 Marv51

Would the API proposal be toward making the event public, with the various compat implications people have raised above? Or would this be a Changed event similar to the suggestion by @terrajobst?

API breaking changes are non-starter IMHO. We'd need a proposal that outlines how this desire can be addressed in a compatible fashion. This includes scenarios where people derived from ReadOnlyObservableCollection and subscribed to the existing event.

terrajobst avatar Mar 30 '20 23:03 terrajobst

Idea:

  1. Introduce a new class ReadOnlyObservableList<T>.
  2. We could consider to mark the existing class ReadOnlyObservableCollection<T> as obsolete sometime in future. So that we do not have two classes for the same purpose on the long run.

I have added this class in my library System.Waf.Core: https://github.com/jbe2277/waf/blob/master/src/System.Waf/System.Waf/System.Waf.Core/Foundation/ReadOnlyObservableList.cs

It implements the IReadOnlyObservableList<T> interface which is proposed for .NET as IReadOnlyObservableCollection<T> in this issue: #36945

jbe2277 avatar Dec 22 '20 13:12 jbe2277

How about extension methods to subscribe/unsubscribe? Extension event if those are introduced.

lostmsu avatar Aug 01 '22 19:08 lostmsu

Hi All,

I would like to propose a simple, lightweight approach to address this without any binary or source breaking changes:

We can introduce two new public events inside ReadOnlyObservableCollection<T> — for example, CollectionUpdated, PropertyUpdated or CollectionChangedDirect, PropertyChangedDirect or ContentCollectionChanged, ContentPropertyChanged (we can agree on the exact names later). Instead of creating new backing fields, their custom add and remove accessors will directly forward subscriptions to the underlying notification pipeline.

Why this works perfectly:

  • Zero Memory Overhead: No extra backing fields are allocated in memory.

  • Zero Code Duplication: We don't need to touch OnCollectionChanged or duplicate invocation logic; everything flows through the existing pipeline.

  • 100% Compatible: It keeps legacy subclassing intact while finally giving external consumers a clean, public way to subscribe without explicit interface casting.

aw0lid avatar Jul 05 '26 16:07 aw0lid