Aracılığıyla paylaş


Enum.CompareTo(Object) Yöntem

Tanım

Bu örneği belirtilen nesne ile karşılaştırır ve göreli değerlerin bir göstergesini döndürür.

public:
 virtual int CompareTo(System::Object ^ target);
public int CompareTo (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

Parametreler

target
Object

Karşılaştıracak bir nesne veya null.

Döndürülenler

Int32

Ve bu örneğin targetgöreli değerlerini gösteren imzalı bir sayıdır.

Değer Anlamı
Sıfırdan küçük Bu örneğin değeri değerinden targetküçük.
Sıfır Bu örneğin değeri değerine eşittir target.
Sıfırdan büyük Bu örneğin değeri , veya target nulldeğerinden targetbüyüktür.

Uygulamalar

Özel durumlar

target ve bu örnek aynı türde değildir.

Bu örnek , , Int16, , Int32, Int64, ByteUInt16, UInt32veya UInt64türü SBytedeğildir.

Bu örnek null.

Örnekler

Aşağıdaki örnekte, bağlamında kullanımı CompareTo gösterilmektedir 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
type VehicleDoors =
    | Motorbike = 0
    | Sportscar = 2
    | Sedan = 4
    | Hatchback = 5

let myVeh = VehicleDoors.Sportscar
let yourVeh = VehicleDoors.Motorbike
let otherVeh = VehicleDoors.Sedan

printfn $"Does a {myVeh} have more doors than a {yourVeh}?"
printfn $"""{if myVeh.CompareTo yourVeh > 0 then "Yes" else "No"}\n"""

printfn $"Does a {myVeh} have more doors than a {otherVeh}?"
printfn $"""{if myVeh.CompareTo otherVeh > 0 then "Yes" else "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

Şunlara uygulanır

Ayrıca bkz.