3

I want my interfaces to have property

IReadOnlyCollection<SomeType> Items { get; }

I also want this collection to be observable

INotifyCollectionChanged Items { get; }

But there is no way to declare duplicate properties.

I REALLY searched MSDN, googling for something like ReadOnlyObservableCollection or maybe IObservableCollection and didn't find anything. I really need this type of interface, so I will probably just introduce my own. But I don't want to invent a wheel here.

Question: I am right that there are no default C# interface that combines INotifyCollectionChanged and IEnumerable?

p.s. I know that there is ReadOnlyObservableCollection class out there, but one crucial detail here - it's not covariant like IReadOnlyCollection. And I do want this covariance

  • Returning a INotifyCollectionChanged does not mean the thing will be observable. Whatever implements IReadOnlyCollection has to also implement INotifyCollectionChanged. What you are doing there does does not make any sense for me. – Christopher Jul 30 '19 at 21:01
  • Help me to write it correctly. I want to have one interface for property that will represent IReadOnlyCollection (or IEnumerable) and also fire notifications like INotifyCollectionChanged. This makes sense? – Alexandr Denschikov Jul 30 '19 at 21:57
  • 1
    Take a look at this [post](https://stackoverflow.com/questions/1763696/how-can-i-make-a-read-only-observablecollection-property) -- in particular the answer from @MeirionHughes where he starts with __"I don't like using ReadOnlyObservableCollection as it seems like a mistake / broken class"__ and then offers up a simple alternative in about 20 lines of code. I think he has provided what you may be looking for -- including covariance. – David Tansey Jul 31 '19 at 01:36
  • The same class can implement more than one interface. – 15ee8f99-57ff-4f92-890c-b56153 Jul 31 '19 at 03:56
  • 1
    @DavidTansey YES, that's exactly what I want. And also it really looks like I will create those interfaces myself. My hope was that I missed something like that in some Microsoft namespaces. Because it seemed like a pretty useful contract to have in default libraries. – Alexandr Denschikov Jul 31 '19 at 12:22

0 Answers0