ReadOnlyObservableCollection.CollectionChanged should be made public
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.
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.
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.
@nguerrera @terrajobst, thoughts?
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.
+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?
See my comment on dotnet/corefx#1029. This is actually binary breaking.
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.
Anyone willing to write up a formal api proposal ?
Any updates on this issue?
We would welcome an API proposal towards addressing this issue.
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?
Would the API proposal be toward making the event public, with the various compat implications people have raised above? Or would this be a
Changedevent 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.
Idea:
- Introduce a new class
ReadOnlyObservableList<T>. - 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
How about extension methods to subscribe/unsubscribe? Extension event if those are introduced.
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.