Export (0) Print
Expand All

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
)

Parameters

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.

Value

Condition

Less than zero

This instance precedes value.

Zero

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

Greater than zero

This instance follows value.

-or-

value is null.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
ArgumentException

value is not a String.

value must be a String object.

Caution noteCaution

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. The CompareTo 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 the CompareOrdinal(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()
   {
      TestClass test = new TestClass();
      string s = "sometext";
      try 
      {
         int i = s.CompareTo(test);
      }
      catch (ArgumentException e)
      {
         Console.WriteLine("Error: {0}", e.ToString());
      }
   }
}
// The example displays the following output: 
//    Error: System.ArgumentException: Object must be of type String. 
//       at System.String.CompareTo(Object value) 
//       at Example.Main()

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft