Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are probably several reasons. Here are two off the top:</p> <ol> <li><p><strong>Huge backwards compatibility problems</strong></p> <p>How would you write the definition of <code>ICollection&lt;T&gt;</code>? This looks natural:</p> <pre><code>interface ICollection&lt;T&gt; : IReadOnlyCollection&lt;T&gt; { int Count { get; } } </code></pre> <p>But it has a problem, because <code>IReadOnlyCollection&lt;T&gt;</code> also declares a <code>Count</code> property (the compiler will issue a warning here). Apart from the warning, leaving it as-is (which is equivalent to writing <code>new int Count</code>) allows implementors to have <em>different</em> implementations for the two <code>Count</code> properties by implementing at least one explicitly. This might be "amusing" if the two implementations decided to return different values. Allowing people to shoot themselves in the foot is rather not C#'s style.</p> <p>OK, so what about:</p> <pre><code>interface ICollection&lt;T&gt; : IReadOnlyCollection&lt;T&gt; { // Count is "inherited" from IReadOnlyCollection&lt;T&gt; } </code></pre> <p>Well, this breaks all existing code that decided to implement <code>Count</code> explicitly:</p> <pre><code>class UnluckyClass : ICollection&lt;Foo&gt; { int ICollection&lt;Foo&gt;.Count { ... } // compiler error! } </code></pre> <p>Therefore it seems to me that there's no good solution to this problem: either you break existing code, or you force an error-prone implementation on <em>everyone</em>. So <a href="https://www.google.com/search?q=the%20only%20winning%20move%20is%20not%20to%20play" rel="noreferrer">the only winning move is not to play</a>.</p></li> <li><p><strong>Semantic runtime type checks</strong></p> <p>It would be meaningless to write code like</p> <pre><code>if (collection is IReadOnlyCollection&lt;Foo&gt;) </code></pre> <p>(or the equivalent with reflection) unless <code>IReadOnlyCollection&lt;T&gt;</code> is "opt-in": if <code>ICollection&lt;T&gt;</code> were a superset of <code>IReadOnlyCollection&lt;Foo&gt;</code> then pretty much every collection class under the sun would pass this test, making it not useful in practice.</p></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