共用方式為


Char.CompareTo Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares this instance to a specified object and returns an integer that indicates whether this instance precedes, follows, or has the same position in the sort order as the specified object.

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

Syntax

'Declaration
Public Function CompareTo ( _
    value As Object _
) As Integer
public int CompareTo(
    Object value
)

Parameters

  • value
    Type: System.Object
    An object to compare this instance to, or nulla null reference (Nothing in Visual Basic).

Return Value

Type: System.Int32
A signed number that indicates the position of this instance in the sort order in relation to the value parameter.

Return Value

Description

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 is follows value.

-or-

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

Implements

IComparable.CompareTo(Object)

Exceptions

Exception Condition
ArgumentException

value is not a Char object.

Remarks

The CompareTo method implements the IComparable interface.

The value parameter must be nulla null reference (Nothing in Visual Basic) 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. Any instance of Char, regardless of its value, is considered greater than nulla null reference (Nothing in Visual Basic).

Examples

The following example demonstrates CompareTo.


Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

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

      outputBlock.Text &= chA.CompareTo("A"c)   ' Output: "0" (meaning they're equal & vbCrLf
      outputBlock.Text &= "b"c.CompareTo(chB)   ' Output: "32" (meaning 'b' is 32 greater than 'B' & vbCrLf
      outputBlock.Text &= chA.CompareTo(chB)    ' Output: "-1" (meaning 'A' is less than 'B' by 1 & vbCrLf

   End Sub

End Module
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      char chA = 'A';
      char chB = 'B';

      outputBlock.Text += chA.CompareTo('A') + "\n";    // Output: "0" (meaning they're equal)
      outputBlock.Text += 'b'.CompareTo(chB) + "\n";    // Output: "32" (meaning 'b' is greater than 'B' by 32)
      outputBlock.Text += chA.CompareTo(chB) + "\n";    // Output: "-1" (meaning 'A' is less than 'B' by 1)
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.