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.