Take the 2-minute tour ×

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 ?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

Can I use covariance and contravariance of generic interfaces and delegates with anonymous types?

Yes. Anonymous types are reference types; variance only works with reference types.


Interface covariance:

var sequenceOfAnonymous = from c in customers select new {c.Name, c.Age};
var sequenceOfObject = (IEnumerable<object>)sequenceOfAnonymous;

Array covariance:

var arrayOfAnonymous = sequenceOfAnonymous.ToArray();
var arrayOfObject = (object[]) arrayOfAnonymous;

To demonstrate delegate covariance you need to use a generic type inference trick:

static Func<R> MakeFunc(Func<R> f) { return f; }
var funcOfAnonymous = MakeFunc( ()=>new { X = 123 } );
var funcOfObject = (Func<object>)funcOfAnonymous;

Interface contravariance needs a slightly different trick: casting by example:

interface IFrobber<in T> { void Frob(T t); }
class Frobber<T> : IFrobber<T> 
    public void Frob(T t) { Console.WriteLine(t); }
static IFrobber<T> FrobByExample<T>(IFrobber<T> frobber, T example) 
{ return frobber; }
var frobberOfObject = new Frobber<object>();
var frobberOfAnonymous = FrobByExample(frobberOfObject, new { X = 0 });

And similarly for delegate contravariance:

static Action<A> ActionByExample<A>(Action<A> action, A example) 
{ return action; }
var actionOfObject = (Action<object>) x => { Console.WriteLine(x); }
var actionOfAnonymous = ActionByExample(actionOfObject, new { X = 0 } );

Make sense?

share|improve this answer
Thanks! There are good examples and clear my doubts. –  Turbot Mar 17 '12 at 23:27
add comment

Your Answer


