Felix_Jiang <Fe********@discussions.microsoft.com> wrote:
The following code returns different result when running on dotnetFramework
1.1 and 2.0. For 1.1, it returns "Object". For 2.0, it returns "String". Can
anyone explain to me why? Thanks!
It's because in C# 1.1, delegate signatures had to match *precisely*,
including return value.
In C# 2.0, delegate signatures can have covariance/contravariance of
return type/parameter types (not necessarily respectively - I can never
remember which way round it is). In other words, if you have a delegate
declared as:
delegate object DoSomething (string x)
and a method
Stream MyMethod (object o)
then you can do:
DoSomething x = new DoSomething (MyMethod);
whereas that would have worked in C# 1.1.
Now, to come to your example - that means that in C# 1.1, only one of
the two methods (the object method) could be chosen, so it was. In C#
2.0, both methods are applicable, so the compiler chooses the most
specific version (in terms of overriding, I believe - I'd need to look
at the spec for the details).
It's an interesting case of breaking backwards compatibility though - I
hadn't come across that before.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too