471,337 Members | 877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Same C# code returns different result on dotnetFramework 1.1 and 2

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!

=========================================
using System;

delegate object D();

class A {
public object M() {return "object";}
}

class B:A {
public string M() {return "string";}
}

public class MyClass {
public static void Main() {
B b = new B();
D d = new D(b.M);
Console.WriteLine(d());
}
}
=========================================
Jan 22 '06 #1
3 1289
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
Jan 22 '06 #2
Jon, thanks for you reply, that really helps. I check the spec and it is
covariance this situation. And I think it will be the same affect for the
contravariance. The code of below demonstrate this.
================================================== ====
using System;

delegate void D(string s);

class A {
public void M(string s) {Console.WriteLine("string");}
}

class B:A {
public void M(object o) {Console.WriteLine("object");}
}

class Program {
public static void Main() {
B b = new B();
D d = new D(b.M);
d("Message");
}
}
================================================== ====
The result will be "Object" on 2.0 while "String" on 1.1. I learned from
your thread that for covariance, the compiler chooses the most specific
version. But for contravariance, it seems to be opposite that compiler
chooses the most
general version instead.

Am I correct? Thanks!!!
Jan 22 '06 #3
Felix_Jiang <Fe********@discussions.microsoft.com> wrote:
Jon, thanks for you reply, that really helps. I check the spec and it is
covariance this situation. And I think it will be the same affect for the
contravariance. The code of below demonstrate this.
<snip>
The result will be "Object" on 2.0 while "String" on 1.1. I learned from
your thread that for covariance, the compiler chooses the most specific
version. But for contravariance, it seems to be opposite that compiler
chooses the most
general version instead.

Am I correct? Thanks!!!


I believe it's not a case of most specific in terms of
parameters/return value - it's returning the one which is a member of
the "most derived" type. However, that's still just a suspicion. I'd
have to check the spec to be sure.

--
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
Jan 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by James Aguilar | last post: by
11 posts views Thread by persevere | last post: by
6 posts views Thread by CJ | last post: by
5 posts views Thread by othman taj via .NET 247 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.