Enum.CompareTo(Object) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。
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
パラメーター
- target
- Object
比較対象のオブジェクト、または null
。
戻り値
このインスタンスと target
の相対値を示す符号付き数値。
値 | 説明 |
---|---|
0 より小さい値 | このインスタンスの値が target より小さい値です。
|
ゼロ | このインスタンスの値が target の値と同じです。
|
0 より大きい値 | このインスタンスの値が target の値より大きいか、target が null です。 |
実装
例外
target
とこのインスタンスは同じ型ではありません。
このインスタンスは null です。
例
次の例は、Enum
コンテキストでCompareTo
を使用する方法を示しています。
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
適用対象
こちらもご覧ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET