I saw Jon Skeet’s lecture at the NDC 2010
He mentioned something interesting :
public Class Base
{
public void Foo(IEnumerable<string> strings){}
}
public Class Child:Base
{
publc void Foo(IEnumerable<object> objects) {}
}
Main :
List<string> lst = new List<string>();
lst.Add("aaa");
Child c = new Child();
c.Foo(lst);
With C# 3 it will call : Base.Foo
With C# 4 it will call : Child.Foo
I know it’s because covariance
Question :
Isn’t it a bit code breaking change ?
Is there any workaround so this code will continue work as it was on ver 3?
Yes, it’s a breaking change. Any time you make a prevously-invalid conversion legal, it’s a breaking change.
Unfortunately, it’s very hard to add features to the language without making any breaking changes. There are a few more around events in C# 4 if you really want to look for them. It’s unlikely that these will affect most developers, of course.
There were similar breaking changes between C# 1 and C# 2, where the implementation used would have changed between different versions for this code:
In this case the compiler actually gives a warning: