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

Объект для сравнения или значение null.An object to compare, or null.

Возвращаемое значение

Int32

Знаковое число, представляющее относительные значения этого экземпляра и параметра 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.
ZeroZero Значение данного экземпляра равно значению параметра target.The value of this instance is equal to the value of target.
Больше нуляGreater than zero Значение этого экземпляра больше значения параметра target или параметр target имеет значение null.The value of this instance is greater than the value of target, or target is null.

Реализации

Исключения

target и этот экземпляр не принадлежат к одному типу.target and this instance are not the same type.

Этот экземпляр не принадлежит к типу SByte, Int16, Int32, Int64, Byte, UInt16, UInt32 или UInt64.This instance is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.

Этот экземпляр имеет значение NULL.This instance is null.

Примеры

В следующем примере показано использование CompareTo в контексте Enum .The 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

Применяется к

См. также раздел