Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot do the... </p> <pre><code>ItemCollection&lt;Feature&gt; features = ...; ItemCollection&lt;Item&gt; items = features; </code></pre> <p>It's about generics <code>variance</code> (covariance and contravariance that is) - <strong>and it's only supported for interfaces, delegates - and only providing they're designed that way</strong> (decorated with <code>in</code> / <code>out</code> - and conforming to the rules that go with it). e.g. <code>IEnumerable&lt;&gt;</code> is (if you lookup its definition you'll see the <code>out</code>). For more details I think it's best to read more... </p> <p><a href="https://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-sharp-4-0/246101#246101">How is Generic Covariance &amp; Contra-variance Implemented in C# 4.0?</a><br> <a href="https://stackoverflow.com/questions/2719954/understanding-covariant-and-contravariant-interfaces-in-c-sharp/2720046#2720046">Understanding Covariant and Contravariant interfaces in C#</a><br> <a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd799517.aspx</a> </p> <p>In your case, you could design an <code>IItemCollection&lt;out T&gt;</code> (<code>interface</code>) - that (<strong>theoretically</strong>) might support the casting you need. But it'd have to be <code>read-only</code> (<em>a tad simplified and not entirely correct but it's easier to think that way - the rules are a bit more complex</em>). </p> <p>Since you're naming it a 'collection' I'm assuming it's not just for enumerating, viewing the items - i.e. if you have an <code>Add</code> of a sort (which requires <code>input parameters</code> - i.e. <code>contra-variance</code>) that clashes with the 'covariance' you need for the <code>upcasting</code>. Also any other parameters involved might not allow your interface to be made covariant. </p> <p><hr /> Also some other posts I made related... </p> <p><a href="https://stackoverflow.com/questions/15590540/how-to-make-generic-class-that-contains-a-set-of-only-its-own-type-or-subtypes-a/15599634#15599634">How to make generic class that contains a Set of only its own type or subtypes as Children?</a> </p> <p><a href="https://stackoverflow.com/questions/15723317/c-sharp-generic-handlers-what-am-i-misunderstanding/15723829#15723829">C# generic handlers, what am I misunderstanding?</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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