I read about in C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.
I’m wondering can I use the covariance and contravariance for Anonymous Types (which are class types derive directly from object) and how would that works ?
Yes. Anonymous types are reference types; variance only works with reference types.
Interface covariance:
Array covariance:
To demonstrate delegate covariance you need to use a generic type inference trick:
Interface contravariance needs a slightly different trick: casting by example:
And similarly for delegate contravariance:
Make sense?