473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1395
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
11
by: persevere | last post by:
#include <stdio.h> void main ( void ) { int *p, *q; p = q + 200; printf("%d %d %d %d", p, q, (int)p, (int)q); } The result is, "400 1439 0 1439".
6
by: CJ | last post by:
Okay, same program, different issue. Thanks to the help that I was given I was able to complete my program to find variables in a list that were repeated, and display them once, and how many times...
5
by: othman taj via .NET 247 | last post by:
Hi everyone, I want to encrypt and decrypt a message. I'm using RSA encryption/decryption with PRIVATEKEYBLOB, no hash, and everything works fine, ie I can see my plaintext. I notices that...
1
by: Tom | last post by:
Hi I installed windows 2000 server, SP4, IE6 SP1, Dotnetframework 1.1, VS.net Prerequisites. I created a new site http://localhost:890 I added a new solution and web project in VS.NET 2003....
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.