Enum::CompareTo Method (Object^)
.NET Framework (current version)
Compares this instance to a specified object and returns an indication of their relative values.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- target
-
Type:
System::Object^
An object to compare, or null.
Return Value
Type: System::Int32A signed number that indicates the relative values of this instance and target.
Value | Meaning |
|---|---|
Less than zero | The value of this instance is less than the value of target. |
Zero | The value of this instance is equal to the value of target. |
Greater than zero | The value of this instance is greater than the value of target. -or- target is null. |
Implements
IComparable::CompareTo(Object^)| Exception | Condition |
|---|---|
| ArgumentException | target and this instance are not the same type. |
| InvalidOperationException |
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
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Show: