Export (0) Print
Expand All

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
)
public final int CompareTo (
	Object target
)
public final function CompareTo (
	target : Object
) : int

Parameters

target

An object to compare, or a null reference (Nothing in Visual Basic).

Return Value

A signed number indicating the relative values of this instance and target.

Return Value

Description

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 a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentException

target and this instance are not the same type.

InvalidOperationException

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

The following code sample 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" );
    }
}

import System.*;

public class CompareToTest
{
    static class VehicleDoors
    {
        private int member;

        VehicleDoors(int n)
        {
            member = n;
		}//VehicleDoors

		public String get_Member()
        {
            if (member == 0) {
                return "MotorBike";
            }
            else {
                if (member == 2) {
                    return "Sportscar";
                }
                else {
                    if (member == 4) {
                        return "Sedan";
                    }
                    else {
                        if (member == 5) {
                            return "Hatchback";
                        }
                        else {
                            return "";
                        }
                    }
                }
            }
		}//get_Member

		public int get_Number()
        {
            return member;
		}//get_Number

		public static int Motorbike = 0;
        public static int Sportscar = 2;
        public static int Sedan = 4;
        public static int Hatchback = 5;
    } //VehicleDoors

    public static void main(String[] args)
    {
        VehicleDoors myVeh = new VehicleDoors(VehicleDoors.Sportscar);
        VehicleDoors yourVeh = new VehicleDoors(VehicleDoors.Motorbike);
        VehicleDoors otherVeh = new VehicleDoors(VehicleDoors.Sedan);

        Console.WriteLine("Does a {0} have more doors than a {1}?", 
            myVeh.get_Member(), yourVeh.get_Member());
        Console.WriteLine("{0}{1}", 
            (String.Compare(System.Convert.ToString(myVeh.get_Number()), 
            System.Convert.ToString(yourVeh.get_Number())) > 0) ? "Yes" : "No", 
            Environment.get_NewLine());

        Console.WriteLine("Does a {0} have more doors than a {1}?", 
            myVeh.get_Member(), otherVeh.get_Member());
        Console.WriteLine("{0}", (
            String.Compare(System.Convert.ToString(myVeh.get_Number()), 
            System.Convert.ToString(otherVeh.get_Number())) > 0) ? "Yes" : "No");
    } //main
} //CompareToTest

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft