Export (0) Print
Expand All

Char.CompareTo Method (Object)

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)

public int CompareTo(
	Object value
)

Parameters

value
Type: System.Object
An object to compare this instance to, or null.

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 null.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
ArgumentException

value is not a Char object.

The CompareTo method implements the IComparable interface.

The value parameter must be null 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 null.

The following example demonstrates CompareTo.


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)
   }
}


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

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

Community Additions

ADD
Show:
© 2014 Microsoft