473,544 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(d());
}
}
=============== =============== ===========
Jan 22 '06 #1
3 1399
Felix_Jiang <Fe********@dis cussions.micros oft.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.co m>
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.WriteL ine("string");}
}

class B:A {
public void M(object o) {Console.WriteL ine("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********@dis cussions.micros oft.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.co m>
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
6175
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 make it better soon. Unfortunately, there is never a non-crucial time in which we can do an upgrade, so we are stuck for now. Point 1: There are...
23
1888
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 'fprintf'. What effect do they have in those parenthesis? I understand how the or is useful and why never to do it, I'm really just asking about that...
11
3090
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
1592
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 they appeared in the list. And it worked great! But, being the perfectionist that I am, I wanted to make the proggie allow any size of list, and...
5
4736
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 cryptEncrypt in cryptoAPI never returns the same ciphertext, can anyone explain me why ? Thanks a lot, Othman --------------------------------
1
1249
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. In the dialog box, the path is http://localhost:8900/ProjectName Then, it showed the error message "Visual Studio .NET has detected that the...
16
2592
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 where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting...
0
1470
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 of the field I want to compare is different, but when all the elements are same, the list still gets sorted(meaning the sequence of the elements...
11
3504
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 returns a elementtree, on which you can then apply xpath; 2) To parse a string (xml section), you can call XML or fromstring, but both return element...
0
7438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
996
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.