Using == or Equals for string comparison
Asked Answered
R

5

12

In some languages (e.g. C++) you can't use operators like == for string comparisons as that would compare the address of the string object, and not the string itself. However, in C# you can use == to compare strings, and it will actually compare the content of the strings. But there are also string functions to handle such comparisons, so my question is; should you?

Given two strings:

string aa = "aa"; 
string bb = "bb";

Should you compare them like this:

bool areEqual = (aa == bb); 

Or should you use the Equal function, like this:

bool areEqual = aa.Equals(bb); 

Is there any technical difference anyway? Or reasonable arguments for best practice?

Rameriz answered 28/12, 2009 at 10:10 Comment(2)
Minor quibble: In C++, std::string can be compared with operator==. It's char[] and char * that can't. The term string is somewhat ambiguous in C++.Barometry
Thanks for clearing that up! It's been a while since I've been doing c++, and obviously my records of it is a bit buzzed..Rameriz
I
25

I wouldn't use:

aa.Equals(bb)

unless I knew aa couldn't possibly be null. I might use:

string.Equals(aa,bb)

But I'd mainly use that it I wanted to use one of the specific StringComparison modes (invariant, ordinal, case-insensitive, etc). Although I might also use the StringComparer implementations, since they are a bit easier to abstract (for example, to pass into a Dictionary<string, Foo> for a case-insensitive ordinal dictionary). For general purpose usage,

a == b

is fine.

Indite answered 28/12, 2009 at 10:12 Comment(0)
O
26

This is the implementation of the operator:

    public static bool operator == (String a, String b) {
       return String.Equals(a, b);
    }

Don't lose any sleep over this.

Offside answered 28/12, 2009 at 10:22 Comment(1)
Now, that's useful to know! :-) No sleep lost anyway - really just wondering if there is a best practice.Rameriz
I
25

I wouldn't use:

aa.Equals(bb)

unless I knew aa couldn't possibly be null. I might use:

string.Equals(aa,bb)

But I'd mainly use that it I wanted to use one of the specific StringComparison modes (invariant, ordinal, case-insensitive, etc). Although I might also use the StringComparer implementations, since they are a bit easier to abstract (for example, to pass into a Dictionary<string, Foo> for a case-insensitive ordinal dictionary). For general purpose usage,

a == b

is fine.

Indite answered 28/12, 2009 at 10:12 Comment(0)
J
2

There is no technical difference (unless aa is null). Use whatever looks better to you. In my opinion, using operator overloads makes the code clearer.

Use functions when you need (or might need in future) their additional arguments (as in CompareTo())

Jowers answered 28/12, 2009 at 10:13 Comment(0)
L
1

Best-practise-wise, I would tend to always use an Equals() function for string comparison. This makes it clear when someone else reads your code that you specifically want the strings compared.

Lysimachus answered 28/12, 2009 at 10:17 Comment(1)
...as opposed to specifically comparing object referencesLysimachus
R
-3

generally speaking, == does pointer equality, while .equals checks whether the attributes are equal. So if you did something like

a = 'a';
b = 'a';
bool c = (a == b);
bool d = (a.Equals(b))

then c should return false and d should be true.

Republicanism answered 28/12, 2009 at 10:20 Comment(4)
It really depends what a and b are typed as here. If they are string it won't compile. If they are char it will be true twice. If they are object it will be false/true. But none of these quite match the question (and you'd need to watch for compiler interning too).Indite
Not neccessarily as .NET uses string interning, which means that that could actually be the same string under the covers.Inescutcheon
@Inescutcheon - that would only apply for string a = "a"; string b = "a";, but yes, I agree.Indite
@Inescutcheon - or more importantly, object a = "a"; object b = "a";Indite

© 2022 - 2024 — McMap. All rights reserved.