Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?
    text
    copied!<p>In .NET 4.5 / C# 5, <code>IReadOnlyCollection&lt;T&gt;</code> is declared with a <code>Count</code> property:</p> <pre><code>public interface IReadOnlyCollection&lt;out T&gt; : IEnumerable&lt;T&gt;, IEnumerable { int Count { get; } } </code></pre> <p>I am wondering, wouldn't it have made sense for <code>ICollection&lt;T&gt;</code> to implement the <code>IReadOnlyCollection&lt;T&gt;</code> interface as well:</p> <pre><code>public interface ICollection&lt;T&gt; : IEnumerable&lt;T&gt;, IEnumerable, *IReadOnlyCollection&lt;T&gt;* </code></pre> <p>This would've meant that classes implementing <code>ICollection&lt;T&gt;</code> would've automatically implemented <code>IReadOnlyCollection&lt;T&gt;</code>. This sounds reasonable to me.</p> <p>The <code>ICollection&lt;T&gt;</code> abstraction can be viewed as an extension of the <code>IReadOnlyCollection&lt;T&gt;</code> abstraction. Note that <code>List&lt;T&gt;</code>, for example, implements both <code>ICollection&lt;T&gt;</code> and <code>IReadOnlyCollection&lt;T&gt;</code>.</p> <p>However it has not been designed that way. </p> <p>What am I missing here? Why would the current implementation have been chosen instead?</p> <hr> <p><strong>UPDATE</strong></p> <p>I'm looking for an answer that uses <strong>Object Oriented design reasoning</strong> to explain why:</p> <ul> <li>A concrete class such as <code>List&lt;T&gt;</code> implementing both <code>IReadOnlyCollection&lt;T&gt;</code> <em>and</em> <code>ICollection&lt;T&gt;</code></li> </ul> <p><em>is a better design than:</em></p> <ul> <li><code>ICollection&lt;T&gt;</code> implementing <code>IReadOnlyCollection&lt;T&gt;</code> directly</li> </ul> <hr> <p>Also please note that this is essentially the same question as:</p> <ol> <li>Why doesn't <code>IList&lt;T&gt;</code> implement <code>IReadOnlyList&lt;T&gt;</code>?</li> <li>Why doesn't <code>IDictionary&lt;T&gt;</code> implement <code>IReadOnlyDictionary&lt;T&gt;</code>?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload