Boxing confuses MS VS 2003 also

A

Aamir Mahmood

Hi
I am sure that this is a bug in MS VS 2003;
Consider the following code:
----
public static void Main() {
int i=1;
int j=1;

object a=1;
object b=1;

object c=i;
object d=j;

object e=i;
object f=i;

object g=b;
object h=b;

if (a==b) {
Console.WriteLine("a==b True");
} else {
Console.WriteLine("a==b False");
}
if (c==d) {
Console.WriteLine("c==d True");
} else {
Console.WriteLine("c==d False");
}
if (i==j) {
Console.WriteLine("i==j True");
} else {
Console.WriteLine("i==j False");
}
if (e==f) {
Console.WriteLine("e==f True");
} else {
Console.WriteLine("e==f False");
}
if (g==h) {
Console.WriteLine("g==h True");
} else {
Console.WriteLine("g==h False");
}
}
----
Add watches for the following expressions
a==b
c==d
e==f
g==h
and run the code in Debug mode.

You will see that the watch window shows the values of these expressions as
'True' (also 'True' in the QuickWatch Window) but the debugging always falls
through the 'else' part in the comparison. Heh...? What is this?

Is this a bug...?

I agian say that MS has wrongly implemented the comparison for boxed
variables.

-
Aamir
 
W

Wessel Troost

You will see that the watch window shows the values of these expressions
as
'True' (also 'True' in the QuickWatch Window) but the debugging always
falls
through the 'else' part in the comparison. Heh...? What is this?

You are right, the VS2003 debugger appearantly does a different == than
you would expect.

The VS2005B2 debugger shows it correctly:

a==b --> False, boxing int gives different object each time
c==d --> False, boxing int gives different object each time
e==f --> False, boxing int gives different object each time
g==h --> True, both objects contain reference to B.

Greetings,
Wessel
 
N

Nick Malik [Microsoft]

As Wessel points out, the debugger watch window has this bug, but the code
is executing correctly. The debugger has been fixed in Whidbey.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top