Information
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
)
public final int CompareTo (
	Object value
)
public final function CompareTo (
	value : Object
) : int
Not applicable.

Parameters

value

An Object that evaluates to a String.

Return Value

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 a null reference (Nothing in Visual Basic).

Exception typeCondition

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.

The following code example demonstrates how you can use the CompareTo method with an Object.

using System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static void Main()
      {
      MyClass my = new MyClass();
      string s = "sometext";
      try 
         {
         int i = s.CompareTo(my);
         }
            catch (Exception e) 
            {
            Console.WriteLine("Error: {0}",e.ToString());
            }
      }
}

import System.*;

public class MyClass
{
} //MyClass

public class ArgExceptionExample
{
    public static void main(String[] args)
    {
        MyClass my = new MyClass();
        String s = "sometext";
        try {
            int i = s.CompareTo(my);
        }
        catch (System.Exception e) {
            Console.WriteLine("Error: {0}", e.ToString());
        }
    } //main
} //ArgExceptionExample

import System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static function Main()
      {
      var my : MyClass = new MyClass();
      var s : String = "sometext";
      try 
         {
         var i : int = s.CompareTo(my);
         }
     catch (e : Exception) 
         {
         Console.WriteLine("Error: {0}",e.ToString());
         }
      }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft