Reputation: 150253
public void Foo (IEnumerable<object> objects)
{
}
var strings = new List<string>{"first", "second", "third"};
Foo(strings); // Compilation Error.
Foo(strings.Cast<object>()); // O.k.
Upvotes: 1
Views: 780
Reputation: 62439
Because it cannot know what you want to do. Same reason why the following line does not compile:
string s = new object();
To force an "unsafe" type cast on the user would be too much liberty given to the compiler.
Upvotes: 0
Reputation: 496
You wan't to look up co-variance and contra-variance.
It's new feature in .NET 4.0
Upvotes: 0