Enum.CompareTo(Object) 方法

定义

将此实例与指定对象进行比较并返回一个对二者的相对值的指示。Compares this instance to a specified object and returns an indication of their relative values.

public:
 virtual int CompareTo(System::Object ^ target);
public int CompareTo (object target);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
Public Function CompareTo (target As Object) As Integer

参数

target
Object

要比较的对象,或为 nullAn object to compare, or null.

返回

一个指示此实例和 target 的相对值的带符号数字。A signed number that indicates the relative values of this instance and target.

Value 含义Meaning
小于零Less than zero 此实例的值小于 target 的值。The value of this instance is less than the value of target.
Zero 此实例的值等于 target 的值。The value of this instance is equal to the value of target.
大于零Greater than zero 此实例的值大于 target 的值。The value of this instance is greater than the value of target. -or- targetnulltarget is null.

实现

异常

target 和此实例不是同一类型。target and this instance are not the same type.

此实例不是类型 SByteInt16Int32Int64ByteUInt16UInt32UInt64This instance is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.

此实例为 NULL。This instance is null.

示例

下面的示例演示如何在 Enum的上下文中使用 CompareToThe following example illustrates the use of CompareTo in the context of Enum.

using namespace System;

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

int 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 );
   Int32 iRes = myVeh.CompareTo( yourVeh );
   Console::WriteLine(  "{0}{1}", iRes > 0 ? (String^)"Yes" : "No", Environment::NewLine );
   Console::WriteLine(  "Does a {0} have more doors than a {1}?", myVeh, otherVeh );
   iRes = myVeh.CompareTo( otherVeh );
   Console::WriteLine(  "{0}", iRes > 0 ? (String^)"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
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
Public Class CompareToTest
    Enum VehicleDoors
        Motorbike = 0
        Sportscar = 2
        Sedan = 4
        Hatchback = 5
    End Enum
    
    Public Shared Sub Main()
        Dim myVeh As VehicleDoors = VehicleDoors.Sportscar
        Dim yourVeh As VehicleDoors = VehicleDoors.Motorbike
        Dim otherVeh As VehicleDoors = VehicleDoors.Sedan
        
        Dim output as String

        If myVeh.CompareTo(yourVeh) > 0 Then output = "Yes" Else output = "No"
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh)
        Console.WriteLine("{0}{1}", output, Environment.NewLine)
        
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh)
        If myVeh.CompareTo(otherVeh) > 0 Then output = "Yes" Else output = "No"
        Console.WriteLine("{0}", output)
    End Sub
End Class
' 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

适用于

另请参阅