In .NET 4.5 / C# 5, IReadOnlyCollection<T>
is declared with a Count
property:
public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
}
I am wondering, wouldn't it have made sense for ICollection<T>
to implement the IReadOnlyCollection<T>
interface as well:
public interface ICollection<T> : IEnumerable<T>, IEnumerable, *IReadOnlyCollection<T>*
This would've meant that classes implementing ICollection<T>
would've automatically implemented IReadOnlyCollection<T>
. This sounds reasonable to me.
The ICollection<T>
abstraction can be viewed as an extension of the IReadOnlyCollection<T>
abstraction. Note that List<T>
, for example, implements both ICollection<T>
and IReadOnlyCollection<T>
.
However it has not been designed that way.
What am I missing here? Why would the current implementation have been chosen instead?
UPDATE
I'm looking for an answer that uses Object Oriented design reasoning to explain why:
- A concrete class such as
List<T>
implementing both IReadOnlyCollection<T>
and ICollection<T>
is a better design than:
ICollection<T>
implementing IReadOnlyCollection<T>
directly
Also please note that this is essentially the same question as:
- Why doesn't
IList<T>
implement IReadOnlyList<T>
?
- Why doesn't
IDictionary<T>
implement IReadOnlyDictionary<T>
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…