Mnemonics for common C # types

I often forget if I need to use in or out when defining covariance and contravariant typical types. In java, I have a mnemonic PECS (producer extends consumer super) to help me. Do you know similar mnemonics for C #?

+3
source share
3 answers

Didn't they do this for us when they were called in and out, and not covariant and contravariant? Just think: am I pushing values ​​into or out of them? If you are not sure, try to "exit" - it is much more often (and easier to understand).

+3

in in ; out out .

+1

When I don’t remember, I always refer to IEnumerable<out T>(which, of course, I have to remember the signature of this interface ...). You can only get instances of T "out" from IEnumerable<out T>, so they are covariant. If you can only pass instances of T "in" to the interface (or delegate which is more common), this is contravariant.

0
source

Source: https://habr.com/ru/post/1761955/


All Articles