473,394 Members | 1,715 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,394 software developers and data experts.

string == and Equals

Hi

Is there any way that the following to statements could give different
results?

I have 2 strings (s1 and s2), which if I compare with == are not equal, but
if I compare with string.Equals, then they are equal.

string.Equals(s1, s2) is true

s1 == s2 is false
All this happens really deep down in an application I am testing. So I
haven't yet worked out what is going on, I'd just like a bit of sanity
feedback (I always though that == and Equals on strings were the same).
Thanks,
Peter
Jan 24 '08 #1
9 11774
Peter K <xd****@hotmail.comwrote in
news:eC**************@TK2MSFTNGP05.phx.gbl:
Is there any way that the following to statements could give different
results?

I have 2 strings (s1 and s2), which if I compare with == are not
equal, but if I compare with string.Equals, then they are equal.

string.Equals(s1, s2) is true

s1 == s2 is false
I have now found out why. Of course the objects are "objects" not
"strings". Even though they are string values, they are of type object - I
just hadn't seen that.
Jan 24 '08 #2
On Jan 24, 11:13 am, Peter K <xdz...@hotmail.comwrote:

<snip>
string.Equals(s1, s2) is true
s1 == s2 is false

I have now found out why. Of course the objects are "objects" not
"strings". Even though they are string values, they are of type object - I
just hadn't seen that.
I suspect what you mean is that either s1, s2 or both of them are
declared as type object. If they were declared as string variables,
you'd see the correct result.

Jon
Jan 24 '08 #3
Jon Skeet [C# MVP] <sk***@pobox.comwrote in
news:MP*********************@msnews.microsoft.com:
Peter K <xd****@hotmail.comwrote:
>Yes, both s1 and s2 are of type object.

Are they both known to be strings? If so, that's the easiest way to fix
it - declare them as string variables instead of object variables, and
it'll work fine.
I don't know if they are "always" strings. I don't understand the entirity
of the program, or how it is used :-(

I've sent a begging email to the original programmer, asking about this
(and hoping I can make this simple change), and I hope he replies :-)

Thanks,
Peter
Jan 24 '08 #4
In that case, try the static "object.Equals(x,y)" method... this tests
for referential equality, and (if not) checks whether they both are
non-null, then uses x.Equals(y)

It is probably your best bet if you don't know the types and aren't
using generics.

Marc
Jan 24 '08 #5
Hi,
A dirty workaround is trying to convert them to string:
string s = o as string;

string s1 = o1 as string;

and then check if any of them is null, if so at least one of them was not
an string
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Peter K" <xd****@hotmail.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Jon Skeet [C# MVP] <sk***@pobox.comwrote in
news:MP*********************@msnews.microsoft.com:
>Peter K <xd****@hotmail.comwrote:
>>Yes, both s1 and s2 are of type object.

Are they both known to be strings? If so, that's the easiest way to fix
it - declare them as string variables instead of object variables, and
it'll work fine.

I don't know if they are "always" strings. I don't understand the entirity
of the program, or how it is used :-(

I've sent a begging email to the original programmer, asking about this
(and hoping I can make this simple change), and I hope he replies :-)

Thanks,
Peter

Jan 24 '08 #6
On Thu, 24 Jan 2008 03:21:01 -0800, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
I suspect what you mean is that either s1, s2 or both of them are
declared as type object. If they were declared as string variables,
you'd see the correct result.
And please, remind me again, why is it that the == operator doesn't just
automatically use Object.Equals()?

I'm sure there's a good reason, but it's escaping me at the moment. It
seems pretty relevant to the question though.

Pete
Jan 24 '08 #7
I'm sure there's a good reason, but it's escaping me at the moment. *It *
seems pretty relevant to the question though.

Pete
Oh you men that theae are too red cars and you ask
are both cars red? = true
are they the same car?= false
//CY
Jan 24 '08 #8
On Jan 25, 9:20 am, christ...@gmail.com wrote:

<snip>
Well It wont work with reserved words (I think)... didnt try it but
pretty shure, == and != arent reserved as I remember.. just operators
and operators can be overloaded
Yes, but that's just begging Peter's question as to *why* they can be
overloaded, rather than the compiler just generating code which calls
object.Equals(x, y). Reference equality is rarely useful (and can be
explicitly checked with object.ReferenceEquals).

Overloading == is nice for value types of course, as it avoids boxing.

For reference types, I suspect there aren't many situations where
you'd want == and != to behave differently to a call to object.Equals.
I suppose it avoids a virtual call, which could be slightly handy in
terms of performance... on the other hand, it *makes* the call non-
virtual, which can be a pain. Mind you, equality comparisons up and
down a type hierarchy are painful anyway.

Jon
Jan 25 '08 #9
Yeah but for mocking up a project if you get pissed this is great...
the wow/ick code shows just what (has been sold out by) letting
operators to be overloaded causes

still I think there is the q
is two cars red
and is those the same car?
are we looking at values or references...

But Im not too shure I understood the q.

Think my english is mysterius? I can answwe in mienkieli instead...

//CY Who tooh away chr$(12)?
Jan 26 '08 #10

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

Similar topics

19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
5
by: John Grandy | last post by:
Is there any way in C# to mimic the T-SQL EXEC() command (which runs a string as if it was in-line T-SQL code) ?
10
by: Eranga | last post by:
I have the following code; string test1 = words;//"Exclud" string test2 ="Exclud"; string test3 = String.Copy(words);//"Exclud" bool booTest1 = test1.Equals(test2);//false bool booTest2 =...
4
by: Randy | last post by:
Hello, In comparing strings, is it just as well to use... string1 == string2 or is it better for any reason to use the String.Compare of String.Equals? Thanks
8
by: Ken Wilson | last post by:
In spite of the obvious advantage of not encountering a NullReferenceException unexpectedly in your running program is there an offset cost in performance for using String.IsNullOrEmpty() over !=...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
9
by: Usman Jamil | last post by:
Hi I'm having a strange error while comparing two strings. Please check the code below. This is a simple string comparison code and works just fine on all of my machines. While debugging an...
4
by: priyanka | last post by:
Hi, I want to input a string from command line. I use the following program to input the string. #include<stdio.h> int main(){ char input;
2
by: sukatoa | last post by:
1. when we initialize value, String st = new String("the quick brown fox jump over the lazy dog"); and String st = "the quick brown fox jump over the lazy dog"; both of them are...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...

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.