views:

107

answers:

1

This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ??

Hope I am not confusing! Thanks for the answers in advance!

+5  A: 

IEnumerable isn't contra-variant. It's covariant.

From MSDN (IEnumerable<(Of <(T>)>) Interface) we have that:

Type Parameters

out T

The type of objects to enumerate. This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived.

From this post we have that:

The Base Class Library has been updated to support covariance and contravariance in various commonly used interfaces. For example, IEnumerable is now a covariant interface – IEnumerable.

Sample code:

// Covariant parameters can be used as result types
interface IEnumerator<out T>
{
     T Current { get; }

     bool MoveNext();
}

// Covariant parameters can be used in covariant result types 
interface IEnumerable<out T>
{
     IEnumerator<T> GetEnumerator();
}

// Contravariant parameters can be used as argument types 
interface IComparer<in T>
{
     bool Compare(T x, T y); 
}

For more examples on this, take a look at:

Covariance and Contravariance FAQ

Covariance and Contravariance in C#, Part One (Great series of posts by Eric Lippert about Covariance And Contravariance)

Understanding C# Covariance And Contravariance (3) Samples

Leniel Macaferi
More to the point: the blog entry referenced in the question is incorrect. The writer confused co- and contra-variance. Which is easy to do, IMO...
Stephen Cleary
Exactly. Brad contradicts himself in the post.
Leniel Macaferi
That is what I thought. However, since the blog post mentioned it came from Anders -- the C# Architect and Guru, I was doubtful ! Thanks for clarifying it.
GBackMania