44

Possible Duplicate:
Differences in string compare methods in C#

In .NET there are many string comparison methods, I just want to confirm which one is the best to use considering performance.

string.Equals()

string.Compare()

string.CompareTo()

string.CompareOrdinal()

string.ReferenceEquals()

if (str1 == str2)
Community
  • 1
  • 1
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
  • 1
    I think this has been asked before... – alex May 25 '11 at 06:03
  • 3
    Not a full answer, but beware `a.Equals(b)`, as if a is `null` then it will throw a `NullReferenceException`, whereas `a == b` won't. – Jackson Pope May 25 '11 at 06:05
  • This seems to be the same as the following question: http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c To add to the answers given there, you can also look at http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx by Jon Skeet – IAmTimCorey May 25 '11 at 06:09
  • 1
    BTW: "ABC".Equals(abc_string) is safer than abc_string.Equals("ABC") – Random May 25 '11 at 06:09
  • String.CompareOrdinal is faster (more efficient) when you don't need to do a culture-sensitive comparison, and you do want to do a case-sensitive comparison. – Sachin Prasad Jun 18 '15 at 11:10
  • 2
    This is not a duplicate of the marked question. The other question doesn't mention CompareOrdinal at all, and neither does the answer. – SeeJayBee Sep 28 '15 at 14:18

1 Answers1

30

Ripped from msdn

string.Equals

Determines whether this instance and a specified object, which must also be a String object, have the same value.

string.Compare Compares two specified String objects and returns an integer that indicates their relative position in the sort order.

string.CompareTo Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.

string.CompareOrdinal Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

String equality operators The predefined string equality operators are:

bool operator ==(string x, string y); bool operator !=(string x, string y); Two string values are considered equal when one of the following is true:

Both values are null. Both values are non-null references to string instances that have identical lengths and identical characters in each character position. The string equality operators compare string values rather than string references. When two separate string instances contain the exact same sequence of characters, the values of the strings are equal, but the references are different. As described in Section 7.9.6, the reference type equality operators can be used to compare string references instead of string values.

crypted
  • 10,118
  • 3
  • 39
  • 52
  • Can someone explain this `When two separate string instances contain the exact same sequence of characters, the values of the strings are equal, but the references are different.` to me. – Vinod Srivastav Nov 16 '18 at 09:47
  • 8
    @VinodSrivastav John Smith was born 1st Jan 1980. Another person called John Smith was born 29th December 1999. Two separate people, but exact same name. This is how two separate strings have the same characters, but different references. – The Aurochs Dec 11 '18 at 11:06