Note that there are some explanatory texts on larger screens.

plurals
  1. POstill confused about covariance and contravariance & in/out
    primarykey
    data
    text
    <p>ok i read a bit on this topic on stackoverflow, watched <a href="http://msdn.microsoft.com/en-us/vcsharp/ee672319.aspx" rel="nofollow noreferrer">this</a> &amp; <a href="http://channel9.msdn.com/posts/bruceky/Whirlwind-13-Whats-new-in-C-4-Covariance--Contravariance/" rel="nofollow noreferrer">this</a>, but still a bit confused about co/contra-variance. </p> <p>from <a href="https://stackoverflow.com/questions/1078423/c-is-variance-covariance-contravariance-another-word-for-polymorphism/1078469#1078469">here</a></p> <blockquote> <p>Covariance allows a "bigger" (less specific) type to be substituted in an API where the original type is only used in an "output" position (e.g. as a return value). Contravariance allows a "smaller" (more specific) type to be substituted in an API where the original type is only used in an "input" position.</p> </blockquote> <p>i know it has to do with type safety. </p> <p>about the <code>in/out</code> thing. can i say i use <code>in</code> when i need to write to it, and <code>out</code> when its read only. and <code>in</code> means contra-variance, <code>out</code> co-variance. but from the explanation above... </p> <p>and <a href="https://stackoverflow.com/questions/1078423/c-is-variance-covariance-contravariance-another-word-for-polymorphism">here</a></p> <blockquote> <p>For example, a <code>List&lt;Banana&gt;</code> can't be treated as a <code>List&lt;Fruit&gt;</code> because <code>list.Add(new Apple())</code> is valid for List but not for <code>List&lt;Banana&gt;</code>.</p> </blockquote> <p>so shouldn't it be, if i were to use <code>in</code>/ am going to write to the object, it must be bigger more generic. </p> <p>i know this question has been asked but still very confused. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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