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());
}
}
=============== =============== =========== 3 1423
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
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!!!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 multiple tables: students, courses, cross-reference
|
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 construction "(fprintf(stderr, "Can't open file.\n"),
exit(0), 1))".
---- CODE ----
|
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".
|
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 not have to be recoded every time. So step
one was to not make the program reliant on the list...
|
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
--------------------------------
| |
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 specified Web server is not running ASP.NET version 1.1. You will be unable to run ASP.NET Web...
|
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
language. The idea, apparently, is that the PHP interpreter writes all
the error messages that are...
|
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 still change).
For eg.
Say the list is filled with object of type A, where in A has a
property...
|
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 instead of elementtree. This alone is bad. To make it worse,
you have to create an elementtree from...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |