Decimal.CompareTo Method (Object)
.NET Framework (current version)
Compares this instance to a specified object and returns a comparison of their relative values.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
-
Type:
System.Object
The object to compare with this instance, or null.
Return Value
Type: System.Int32A signed number indicating the relative values of this instance and value.
Return value | Meaning |
|---|---|
Less than zero | This instance is less than value. |
Zero | This instance is equal to value. |
Greater than zero | This instance is greater than value. -or- value is null. |
Implements
IComparable.CompareTo(Object)| Exception | Condition |
|---|---|
| ArgumentException | value is not a Decimal. |
The following code example compares several Decimal and other objects to a reference Decimal value using the CompareTo method.
// Example of the decimal.CompareTo and decimal.Equals instance // methods. using System; class DecCompToEqualsObjDemo { // Get the exception type name; remove the namespace prefix. public static string GetExceptionType( Exception ex ) { string exceptionType = ex.GetType( ).ToString( ); return exceptionType.Substring( exceptionType.LastIndexOf( '.' ) + 1 ); } // Compare the decimal to the object parameters, // and display the object parameters with the results. public static void CompDecimalToObject( decimal Left, object Right, string RightText ) { Console.WriteLine( "{0,-46}{1}", "object: "+RightText, Right ); Console.WriteLine( "{0,-46}{1}", "Left.Equals( object )", Left.Equals( Right ) ); Console.Write( "{0,-46}", "Left.CompareTo( object )" ); try { // Catch the exception if CompareTo( ) throws one. Console.WriteLine( "{0}\n", Left.CompareTo( Right ) ); } catch( Exception ex ) { Console.WriteLine( "{0}\n", GetExceptionType( ex ) ); } } public static void Main( ) { Console.WriteLine( "This example of the decimal.Equals( object ) and \n" + "decimal.CompareTo( object ) methods generates the \n" + "following output. It creates several different " + "decimal \nvalues and compares them with the following " + "reference value.\n" ); // Create a reference decimal value. decimal Left = new decimal( 987.654 ); Console.WriteLine( "{0,-46}{1}\n", "Left: decimal( 987.654 )", Left ); // Create objects to compare with the reference. CompDecimalToObject( Left, new decimal( 9.8765400E+2 ), "decimal( 9.8765400E+2 )" ); CompDecimalToObject( Left, 987.6541M, "987.6541D" ); CompDecimalToObject( Left, 987.6539M, "987.6539D" ); CompDecimalToObject( Left, new decimal( 987654000, 0, 0, false, 6 ), "decimal( 987654000, 0, 0, false, 6 )" ); CompDecimalToObject( Left, 9.8765400E+2, "Double 9.8765400E+2" ); CompDecimalToObject( Left, "987.654", "String \"987.654\"" ); } } /* This example of the decimal.Equals( object ) and decimal.CompareTo( object ) methods generates the following output. It creates several different decimal values and compares them with the following reference value. Left: decimal( 987.654 ) 987.654 object: decimal( 9.8765400E+2 ) 987.654 Left.Equals( object ) True Left.CompareTo( object ) 0 object: 987.6541D 987.6541 Left.Equals( object ) False Left.CompareTo( object ) -1 object: 987.6539D 987.6539 Left.Equals( object ) False Left.CompareTo( object ) 1 object: decimal( 987654000, 0, 0, false, 6 ) 987.654000 Left.Equals( object ) True Left.CompareTo( object ) 0 object: Double 9.8765400E+2 987.654 Left.Equals( object ) False Left.CompareTo( object ) ArgumentException object: String "987.654" 987.654 Left.Equals( object ) False Left.CompareTo( object ) ArgumentException */
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: