I've just found that .NET Fx now has 3 useful interfaces:
IReadOnlyCollection<T>
IReadOnlyList<T>
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.
The IReadOnlyCollection interface extends the IEnumerable interface and represents a basic read-only collection interface. It also includes a Count property apart from the IEnumerable members as shown in the code snippet given below. IReadOnlyCollection<Product> data = products; int numOfRecords = data.
Try this: List<string> listData = new List<string>(); public IReadOnlyList<string> readOnlyData = listData. AsReadOnly();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With