16

Possible Duplicate:
Why was IEnumerable<T> made covariant in C# 4?

I was taking a look on MSDN for IEnumerable<T> interface definition, and see:

public interface IEnumerable<out T> : IEnumerable

I was wondering why T is defined as out, why not?

public interface IEnumerable<T> : IEnumerable

What is the reason for this?

Community
  • 1
  • 1
cuongle
  • 74,024
  • 28
  • 151
  • 206

4 Answers4

21

More information can be found here.

The out makes the type parameter covariant. That is, you can use either the type or any derived types. Note that out only works this way with generics, it has a different meaning when used in method signatures (though you probably already knew that).

Here is the example taken from the referenced page:

// Covariant interface. 
interface ICovariant<out R> { }

// Extending covariant interface. 
interface IExtCovariant<out R> : ICovariant<R> { }

// Implementing covariant interface. 
class Sample<R> : ICovariant<R> { }

class Program
{
    static void Test()
    {
        ICovariant<Object> iobj = new Sample<Object>();
        ICovariant<String> istr = new Sample<String>();

        // You can assign istr to iobj because 
        // the ICovariant interface is covariant.
        iobj = istr;
    }
}

As you can see, the out in the interface signature allows you to assign an ICovariant<String> to an ICovariant<Object> variable, as String derives from Object. Without the out keyword, you would be unable to do this, as the types would be different.

You can read more about covariance (and the related contravariance) here.

As other answers have pointed out, IEnumerable was only made covariant in .NET 4. Trying to write code such as:

IEnumerable<Object> strings = new List<string>();

will compile in .NET 4 and later versions, but not in previous versions.

Daniel
  • 2,944
  • 3
  • 22
  • 40
7

The out type parameter specifier denotes covariance.

In practice,

If I define two interfaces.

interface ISomeInterface<T>
{
}

interface ISomeCovariantInterface<out T> 
{
}

Then, I implement them like this.

class SomeClass<T> : ISomeInterface<T>, ISomeCovariantInterface<T>
{
}

Then I try to compile this code,

ISomeCovariantInterface<object> covariant = new SomeClass<string>(); // works
ISomeInterface<object> invariant = new SomeClass<string>(); // fails

// Cannot implicitly convert type 'SomeClass<string>' to 'ISomeInterface<object>'.
// An explicit conversion exists (are you missing a cast?)

The is because the covariant interface allows more derived instances, where as, the standard interface does not.

Fiddle Here

Jodrell
  • 34,946
  • 5
  • 87
  • 124
6

Covariance. This allows a collection to be assigned items of a more specific or derived type than what's specified in its generic param.

IEnumerable<T> wasn't always covariant; this was new to .NET 4, and the reason for the change is explained here.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

To achieve this:

class Base {}
class Derived : Base {}

List<Derived> list = new List<Derived>();
IEnumerable<Base> sequence = list;
Dennis
  • 37,026
  • 10
  • 82
  • 150