Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.CompareTo Method (Object)


Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public int CompareTo(
	object value


Type: System.Object

An object that evaluates to a String.

Return Value

Type: System.Int32

A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter.



Less than zero

This instance precedes value.


This instance has the same position in the sort order as value.

Greater than zero

This instance follows value.


value is null.

Exception Condition

value is not a String.

value must be a String object.


The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

For more information about the behavior of this method, see the Remarks section of the String.Compare(String, String) method.

Notes to Callers:

Character sets include ignorable characters. TheCompareTo method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.

using System;

public class Example
   public static void Main()
      string s1 = "ani\u00ADmal";
      object o1 = "animal";

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, o1, s1.CompareTo(o1));
// The example displays the following output:
//       Comparison of 'ani-mal' and 'animal': 0

To recognize ignorable characters in a string comparison, call theCompareOrdinal(String, String) method.

The following example uses the CompareTo method with an Object. Because it attempts to compare a String instance to a TestClass object, the method throws an ArgumentException.

using System;

public class TestClass

public class Example 
   public static void Main()
      var test = new TestClass();
      Object[] objectsToCompare = { test, test.ToString(), 123,
                                    123.ToString(), "some text",
                                    "Some Text" };
      string s = "some text";
      foreach (var objectToCompare in objectsToCompare) {
         try {
            int i = s.CompareTo(objectToCompare);
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i);
         catch (ArgumentException) {
            Console.WriteLine("Bad argument: {0} (type {1})",
// The example displays the following output:
//    Bad argument: TestClass (type TestClass)
//    Comparing 'some text' with 'TestClass': -1
//    Bad argument: 123 (type Int32)
//    Comparing 'some text' with '123': 1
//    Comparing 'some text' with 'some text': 0
//    Comparing 'some text' with 'Some Text': -1

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2017 Microsoft