18

I would like to know, when comparing string in C#? which method is suitable to use and why?

CompareTo() or Equals()?

luk2302
  • 55,258
  • 23
  • 97
  • 137
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158

6 Answers6

23

From MSDN:

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.Equals:

Determines whether two String objects have the same value.

In short, CompareTo is used for sorting. Equals is used to determine equality.

qxn
  • 17,162
  • 3
  • 49
  • 72
18

CompareTo() tells you which, and if, one is greater/less than the other, while Equals() simply tells you if they are equivalent values.

If all you want to know is "are they the same values", you use Equals(). If you need to also know how they compare, use CompareTo()

int a = 50;
int b = 10;

//if you need to know if they are equal:
if(a.Equals(b)){
    //won't execute
}

//this would check if they are equal, as well
if(a.CompareTo(b) == 0){
    //won't execute
}

//if you need to know if a is bigger than b, specifically:
if(a.CompareTo(b) > 0){
    //will execute
}

//this would check to see if a is less than b
if(a.CompareTo(b) < 0){
    //won't execute
}

Finally, note that these Equals() and CompareTo() methods are not strictly needed for primitive types like int, because the standard comparison operators are overloaded, so you could do these:

//this would check if they are equal, as well
if(a == b){
    //won't execute
}

//if you need to know if a is bigger than b, specifically:
if(a > b){
    //will execute
}

//this would check to see if a is less than b
if(a < b){
    //won't execute
}

Finally, you mentioned string in your question. Equals() and CompareTo() work as I have describe for string as well. Just keep in mind the 'comparison' that CompareTo() does on strings is based on alphabetical sorting, so "abcdefg" < "z"

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • thx! so if we just want to see if they are the same values, use Equals() will outperform than CompareTo()? What da the adv. of using Equals? – TheOneTeam Feb 01 '12 at 01:59
  • 2
    There *may* be some tiny performance difference, I guess. If you just need to know equality, yes; use `Equals()`. It's also more clear in your code to check `a.Equals(b)` than `a.CompareTo(b) == 0` – Andrew Barber Feb 01 '12 at 02:06
  • 1
    The real advantage is that not objects provide a `CompareTo` method. `Equals` will always be there, as it's provided by the base `Object` class. – Cody Gray - on strike Feb 01 '12 at 03:20
6

The functionality in CompareTo is actually a superset of functionality of Equals. A CompareTo function dictates ordering, before, after or equals while the Equals function merely dictates equality. Hence it's actually possible to define Equals in terms of CompareTo

public bool Equals(string other) {
  return 0 == CompareTo(other);
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    Although a common case, this isn't always the case. A typical example is where `CompareTo()` only checks the name of an object to determine ordering while `Equals()` might check other fields as well. From [MSDN](http://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx): "This current instance occurs in the same position in the sort order as the object specified by the CompareTo method." - it does not say that the object is equal. – larsmoa Aug 20 '14 at 12:18
5

Equals will return a boolean for equality.

CompareTo will return an int, with -1 (or any other negative value) for "less than", 0 for "equals", or 1 (or any other positive value) for "greater than". This method is useful for sorting algorithms.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
1

Equality can be “fussier” than comparison, but not vice versa. CompareTo can say “All objects are equal” while Equals says “But some are more equal than others!”

An example of this is System.String. String’s Equals method and == operator use ordinal comparison, which compares the Unicode point values of each character. Its CompareTo method, however, uses a less fussy culture-dependent comparison. On most computers, for instance, the strings “ṻ” and “ǖ” are different according to Equals, but the same according to CompareTo.

This is from C# in a nutshell

Krishna
  • 1,956
  • 3
  • 15
  • 25
-2

CompareTo method is comparing instance of object with parameter of String object. Equals method determine the value of both are the same or not.

CompareTo should be used when you are comparing two objects' values.

String str1 = "abc";
String str2 = "def"
if(strq.CompareTo(str2) //

Equals should be used when either one or both are not objects.

string str1 = "abc";
if(str1.Equals("abc") //

If you use CompareTo method for normal value type variables, it will use type cast (boxing) which is not necessary.

Thit Lwin Oo
  • 3,388
  • 3
  • 20
  • 23