473,473 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

== operator and Equals for String

I've got code in NUnit which compares two strings.

Object str1 = ...
Object str2 = ...
Assertion.Assert(str1.Equals(str2)); // passes, str1 equals str2
Assertion.Assert(str1 == str2); // fails!

The following link states that str1 == str2 should compare values and
thus == operator should not return false in my case:
http://msdn.microsoft.com/library/de...tyoperator.asp

More magic happens if I work from VisualStudio .NET 2003. If I stop
code execution in debugger for the code like this:

Boolean equals = str1 == str2

I will see that equals is false. If I execute str1 == str2 from
command window in immediate mode I will see true!

Can anybody explain to me what is going on.
Thank you
Jul 19 '05 #1
1 2177
Sergei Gnezdov <se****************@pobox.com> wrote:
I've got code in NUnit which compares two strings.

Object str1 = ...
Object str2 = ...
Assertion.Assert(str1.Equals(str2)); // passes, str1 equals str2
Assertion.Assert(str1 == str2); // fails!

The following link states that str1 == str2 should compare values and
thus == operator should not return false in my case:
No it shouldn't, because your variables are declared as object
references, not string references. Operators are not applied
polymorphically.
More magic happens if I work from VisualStudio .NET 2003. If I stop
code execution in debugger for the code like this:

Boolean equals = str1 == str2

I will see that equals is false. If I execute str1 == str2 from
command window in immediate mode I will see true!

Can anybody explain to me what is going on.


I'd rarely trust the debugger doing this kind of thing, to be honest...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: #Hai | last post by:
Hi, What is the difference between Object.Equals and "==" operator ? When we use CollectionBase.List.Remove(object), which methods is used to compare objects ? Thanks
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
1
by: Sergei Gnezdov | last post by:
I've got code in NUnit which compares two strings. Object str1 = ... Object str2 = ... Assertion.Assert(str1.Equals(str2)); // passes, str1 equals str2 Assertion.Assert(str1 == str2); // fails!...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
2
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
3
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
12
by: mathboy2 | last post by:
Can anyone tell me what I'm doing wrong in the below snippet of code? I'm simply trying to override the == operator but having no luck. I can't even get it to hit a break point at the top of the ==...
0
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
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,...
1
isladogs
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.