C# Char CompareTo(Char)

Description

Char CompareTo(Char) compares this instance to a specified Char object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Char object.

Syntax

Char.CompareTo(Char) has the following syntax.


public int CompareTo(
  char value
)

Parameters

Char.CompareTo(Char) has the following parameters.

  • value - A Char object to compare.

Returns

Char.CompareTo(Char) 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.

Example

The following code example demonstrates CompareTo method.


using System;/*from   w  w w. j  av  a  2s.c om*/

class Sample 
{
    public static void Main() 
    {
        Char i1 = 'A',   i2 = 'A';
        try 
            {
            Console.WriteLine(i1.CompareTo(i2));
        }
        catch (Exception e){
            Console.WriteLine(e);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version