C# Enum CompareTo

Description

Enum CompareTo compares this instance to a specified object and returns an indication of their relative values.

Syntax

Enum.CompareTo has the following syntax.


public int CompareTo(
  Object target
)

Parameters

Enum.CompareTo has the following parameters.

  • target - An object to compare, or null.

Returns

Enum.CompareTo method returns A signed number that indicates the relative values of this instance and target. Value Meaning Less than zero The value of this instance is less than the value of target. Zero The value of this instance is equal to the value of target. Greater than zero The value of this instance is greater than the value of target. -or- target is null.

Example

The following example illustrates the use of CompareTo in the context of Enum.


//w  w w.  j a v a2s.  c o m
using System;
enum Direction { East = 0, West = 2, North = 4, South = 5 };
public class CompareToTest {
    public static void Main() {
        Direction myVeh = Direction.West;
        Direction yourVeh = Direction.East;
        Direction otherVeh = Direction.North;

        Console.WriteLine( myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No");
        Console.WriteLine( myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );
    }
}

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