Decimal::Equals Method (Object^)
Returns a value indicating whether this instance and a specified Object represent the same type and value.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
-
Type:
System::Object^
The object to compare with this instance.
Return Value
Type: System::Booleantrue if value is a Decimal and equal to this instance; otherwise, false.
Notes to Callers:
Compiler overload resolution may account for an apparent difference in the behavior of the two Equals method overloads. If an implicit conversion between the obj argument and a Decimal is defined and the argument is not typed as an Object, compilers may perform an implicit conversion and call the Equals(Decimal) method. Otherwise, they call the Equals(Object^) method, which always returns false if its obj argument is not a Decimal value. The following example illustrates the difference in behavior between the two method overloads. In the case of all primitive integral types, including both signed and unsigned types, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(Decimal) method, whereas the second comparison returns false because the compiler calls the Equals(Object^) method.
The following code example compares several Decimal and other objects to a reference Decimal value using the Equals method.
// Example of the Decimal::CompareTo and Decimal::Equals instance // methods. using namespace System; // Get the exception type name; remove the namespace prefix. 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. void CompDecimalToObject( Decimal Left, Object^ Right, String^ RightText ) { Console::WriteLine( "{0,-46}{1}", String::Concat( "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 ) ); } } int 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 = Decimal(987.654); Console::WriteLine( "{0,-46}{1}\n", "Left: Decimal( 987.654 )", Left ); // Create objects to compare with the reference. CompDecimalToObject( Left, Decimal(9.8765400E+2), "Decimal( 9.8765400E+2 )" ); CompDecimalToObject( Left, Decimal::Parse( "987.6541" ), "Decimal::Parse( \"987.6541\" )" ); CompDecimalToObject( Left, Decimal::Parse( "987.6539" ), "Decimal::Parse( \"987.6539\" )" ); CompDecimalToObject( Left, 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: Decimal::Parse( "987.6541" ) 987.6541 Left.Equals( Object ) False Left.CompareTo( Object ) -1 Object: Decimal::Parse( "987.6539" ) 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 */
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