Export (0) Print
Expand All

Char.CompareTo Method

Compares this instance to a specified object and returns an indication of their relative values.

[Visual Basic]
Public Overridable Function CompareTo( _
   ByVal value As Object _
) As Integer
public virtual int CompareTo(
 object value
public: virtual int CompareTo(
 Object* value
public function CompareTo(
   value : Object
) : int;


An object to compare, or a null reference (Nothing in Visual Basic).

Return Value

A signed number indicating the relative values of this instance and value.

Return Value Description
Less than zero This instance is less than value.
Zero This instance is equal to value.
Greater than zero This instance is greater than value.


value is a null reference (Nothing in Visual Basic).


Exception Type Condition
ArgumentException value is not a Char.


Any instance of Char, regardless of its value, is considered greater than a null reference (Nothing in Visual Basic).

value must be a null reference (Nothing) or an instance of Char; otherwise, an exception is thrown.

The comparison performed by this method is based on the encoded values of this instance and value, not necessarily their lexicographical characteristics.

This method is implemented to support the IComparable interface.


[Visual Basic, C#, C++] The following sample demonstrates CompareTo.

[Visual Basic] 
Imports System

Module CompareToSample

    Sub Main()

        Dim chA As Char
        chA = "A"c
        Dim chB As Char
        chB = "B"c

        Console.WriteLine(chA.CompareTo("A"c))  ' Output: "0" (meaning they're equal)
        Console.WriteLine("b"c.CompareTo(chB))  ' Output: "32" (meaning 'b' is 32 greater than 'B')
    Console.WriteLine(chA.CompareTo(chB))    ' Output: "-1" (meaning 'A' is less than 'B' by 1)

    End Sub

End Module

using System;

public class CompareToSample {
    public static void Main() {
        char chA = 'A';
        char chB = 'B';

        Console.WriteLine(chA.CompareTo('A'));    // Output: "0" (meaning they're equal)
        Console.WriteLine('b'.CompareTo(chB));    // Output: "32" (meaning 'b' is greater than 'B' by 32)
        Console.WriteLine(chA.CompareTo(chB));    // Output: "-1" (meaning 'A' is less than 'B' by 1)

#using <mscorlib.dll>
using namespace System;

int main() {
    char chA = 'A';
    char chB = 'B';

    Console::WriteLine(chA.CompareTo(__box('A')));    // Output: "0" (meaning they're equal)
    Console::WriteLine('b'.CompareTo(__box(chB)));    // Output: "32" (meaning 'b' is greater than 'B' by 32)
    Console::WriteLine(chA.CompareTo(__box(chB)));    // Output: "-1" (meaning 'A' is less than 'B' by 1)

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Char Structure | Char Members | System Namespace | IComparable | Char.Equals

© 2014 Microsoft