C# Char CompareTo(Object)
Description
Char CompareTo(Object)
compares this instance to a specified
object and indicates whether this instance precedes, follows, or appears
in the same position in the sort order as the specified Object.
Syntax
Char.CompareTo(Object)
has the following syntax.
public int CompareTo(
Object value
)
Parameters
Char.CompareTo(Object)
has the following parameters.
value
- An object to compare this instance to, or null.
Returns
Char.CompareTo(Object)
method returns A signed number indicating 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 follows value. -or-
value is null.
Example
The following code example demonstrates CompareTo.
/* ww w . j a v a 2 s.c o m*/
using System;
public class MainClass {
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)
}
}
The code above generates the following result.