Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mnemonic for C# generic types

I often forget if i have to use in or out when defining covarient and contravarient generic types. In java i have the mnemonic PECS (producer extends consumer super) to help me. Do you know a similar mnemonic for c#?

like image 659
mR_fr0g Avatar asked Mar 02 '26 20:03

mR_fr0g


2 Answers

Didn't they do this for us when they called them 'in' and 'out' rather than covariant and contravariant? Just think: am I pushing values 'in', or getting them 'out'? If unsure, try 'out' - it is far more common (and easier to understand).

like image 78
Marc Gravell Avatar answered Mar 05 '26 09:03

Marc Gravell


in types are passed in to functions; out types are returned out from functions.

like image 43
Gabe Avatar answered Mar 05 '26 09:03

Gabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!