16

I've just found that .NET Fx now has 3 useful interfaces:

  1. IReadOnlyCollection<T>
  2. IReadOnlyList<T>
  3. IReadOnlyDictionary<K,V>

And I'm bit confused why HashSet<T> do not implement IReadOnlyCollection<T>? Are there any reasons, or Microsoft just forgot about sets again?

UPD

After two-hours googling I've found that there are many collections in BCL which has .Count property but do not implement IReadOnlyCollection<T> interface.

UPD2

I've found this post http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b4fb991a-3f5c-4923-93d4-7cd5c004f859 and the answer by Immo Landwerth where he've said following

Will other collections besides List<> and Dictionary<> be updated to support these interfaces?

Absolutely. In fact, all of our built-in collection types already implement IReadOnlyList<> and IReadOnlyDictionary<>. This means, you can directly pass an instance of List, T[] or Dictionary<> to an API that takes an IReadOnly-version of it.

hazzik
  • 13,019
  • 9
  • 47
  • 86
  • Ok, so why [List](http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110)) does? – hazzik Aug 07 '12 at 16:18
  • Weird. Imho, an incongruent design decision. See http://www.infoq.com/news/2011/10/ReadOnly-WInRT/ – Colonel Panic Aug 07 '12 at 16:21
  • 12
    @ColonelPanic You misunderstand the intention of `IReadOnlyCollection<>`. Most mutable collections implement this interface. The cool thing about it is that it's **covariant**. Therefore, if you know you're going to only **read** from a collection that goes into a method, you can make the method take an `IReadOnlyCollection`. If someone has a `List`, they can use it as input to your method because of covariance. That's really cool! – Jeppe Stig Nielsen Aug 07 '12 at 16:54
  • Makes sense, cool. So the answer to the OP's question is 'Yes ISet should implement IReadOnlyCollection, this appears to be an oversight by devs' – Colonel Panic Aug 07 '12 at 17:31
  • 10
    Update 2015: Fixed in .NET 4.6 – Colonel Panic Jul 30 '15 at 14:02
  • @JeppeStigNielsen It would be cool if the interface was named `IReadableCollection`. It's not cool to name something in a way that is completely illogical given how it's used. – jpmc26 Sep 24 '15 at 17:08
  • @jpmc26 I agree there is some confusing naming here. If you have a `List<>` instance and you say `.AsReadOnly()` on it, you get a _wrapper_ around your list that will not let you modify the list (others could modify it, so it is not truly immutable). That wrapper class is called `ReadOnlyCollection<>`. So "read-only" is used in another sense there. I kind of like your `IReadableCollection<>` idea. However, it is much too late to fix the confusing names now. – Jeppe Stig Nielsen Sep 25 '15 at 07:55

1 Answers1

16

In version 4.5 of the framework, HashSet<T> does not implement IReadOnlyCollection<out T>.

This omission was resolved in version 4.6 of the framework (released almost 12 months after the above question was asked).

These corrections are not limited to HashSet<T>, other collections such as Stack<T> and Queue<T> have received these improvements.

Speculation on the reason for any omission is moot. It may be oversight or time pressure but frankly, it is of little consequence. I suspect that even direct input from the Microsoft Development Team would be somewhat subjective, even if we enjoy associated anecdotes.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
Jodrell
  • 34,946
  • 5
  • 87
  • 124