473,412 Members | 3,471 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,412 software developers and data experts.

== 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 2176
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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: 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...

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.