This documentation is archived and is not being maintained.

Enum.CompareTo Method

Compares this instance to a specified object and returns an indication of their relative values.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public int CompareTo(
	Object target


Type: System.Object

An object to compare, or null.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and target.

Return Value


Less than zero

The value of this instance is less than the value of target.


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.


target is null.




target and this instance are not the same type.


This instance is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.


This instance is null.

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

using System;

public class CompareToTest {
    enum VehicleDoors { Motorbike = 0, Sportscar = 2, Sedan = 4, Hatchback = 5 };

    public static void Main() {
        VehicleDoors myVeh = VehicleDoors.Sportscar;
        VehicleDoors yourVeh = VehicleDoors.Motorbike;
        VehicleDoors otherVeh = VehicleDoors.Sedan;

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh);
        Console.WriteLine( "{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", Environment.NewLine );

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh);
        Console.WriteLine( "{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );
// The example displays the following output: 
//       Does a Sportscar have more doors than a Motorbike? 
//       Yes 
//       Does a Sportscar have more doors than a Sedan? 
//       No

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0