Enum.CompareTo(Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Compare cette instance à un objet spécifié et retourne une indication de leurs valeurs relatives.
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
Paramètres
- target
- Object
Objet à comparer ou null
.
Retours
Nombre signé qui indique les valeurs relatives de cette instance et de target
.
Value | Signification |
---|---|
Inférieure à zéro | La valeur de cette instance est inférieure à la valeur de target .
|
Zéro | La valeur de cette instance est égale à la valeur de target .
|
Supérieure à zéro | La valeur de cette instance est supérieure à la valeur de target , ou target est null . |
Implémente
Exceptions
target
et cette instance ne sont pas du même type.
Cette instance est null.
Exemples
L’exemple suivant illustre l’utilisation de CompareTo
dans le contexte de 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