Share via


Decimal.Equality Operator

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a value indicating whether two instances of Decimal are equal.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Operator = ( _
    d1 As Decimal, _
    d2 As Decimal _
) As Boolean
public static bool operator ==(
    decimal d1,
    decimal d2
)

Parameters

Return Value

Type: System.Boolean
true if d1 and d2 are equal; otherwise, false.

Examples

The following code example compares several Decimal values to a reference Decimal value using the Equality operator.

' Example of the Decimal relational operators.

Module Example

   Const dataFmt As String = "{0,-47}{1}"

   ' Compare Decimal parameters, and display them with the results.
   Sub CompareDecimals(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As Decimal, ByVal Right As Decimal, _
       ByVal RightText As String)

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "Right: " & RightText, Right) & vbCrLf

      ' The op_Equality, op_GreaterThan, op_GreaterThanOrEqual, 
      ' op_Inequality, op_LessThan, and op_LessThanOrEqual operators 
      ' must be explicitly coded in Visual Basic. If binary =, >, 
      ' >=, <>, <, or <= are used, the Compare method is called.
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_Equality( Left, Right )", _
          Decimal.op_Equality(Left, Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_GreaterThan( Left, Right )", _
          Decimal.op_GreaterThan(Left, Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_GreaterThanOrEqual( Left, Right )", _
          Decimal.op_GreaterThanOrEqual(Left, Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_Inequality( Left, Right )", _
          Decimal.op_Inequality(Left, Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_LessThan( Left, Right )", _
          Decimal.op_LessThan(Left, Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "Decimal.op_LessThanOrEqual( Left, Right )", _
          Decimal.op_LessThanOrEqual(Left, Right)) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim Left As New Decimal(123.456)

      outputBlock.Text &= _
          "This example of the Decimal relational operators " & _
          "generates the " & vbCrLf & "following output. It " & _
          "creates several different Decimal values " & vbCrLf & _
          "and compares them with the following reference " & _
          "value." & vbCrLf & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "Left: Decimal( 123.456 )", Left) & vbCrLf

      ' Create objects to compare with a 2-hour Decimal.
      CompareDecimals(outputBlock, Left, New Decimal(123.456), _
          "Decimal( 1.23456E+2 )")
      CompareDecimals(outputBlock, Left, New Decimal(123.4567), _
          "Decimal( 123.4567 )")
      CompareDecimals(outputBlock, Left, New Decimal(123.4553), _
          "Decimal( 123.4553 )")
      CompareDecimals(outputBlock, Left, New Decimal(123456000, 0, 0, False, 6), _
          "Decimal( 123456000, 0, 0, False, 6 )")
   End Sub
End Module

' This example of the Decimal relational operators generates the
' following output. It creates several different Decimal values
' and compares them with the following reference value.
' 
' Left: Decimal( 123.456 )                       123.456
' 
' Right: Decimal( 1.23456E+2 )                   123.456
' Decimal.op_Equality( Left, Right )             True
' Decimal.op_GreaterThan( Left, Right )          False
' Decimal.op_GreaterThanOrEqual( Left, Right )   True
' Decimal.op_Inequality( Left, Right )           False
' Decimal.op_LessThan( Left, Right )             False
' Decimal.op_LessThanOrEqual( Left, Right )      True
' 
' Right: Decimal( 123.4567 )                     123.4567
' Decimal.op_Equality( Left, Right )             False
' Decimal.op_GreaterThan( Left, Right )          False
' Decimal.op_GreaterThanOrEqual( Left, Right )   False
' Decimal.op_Inequality( Left, Right )           True
' Decimal.op_LessThan( Left, Right )             True
' Decimal.op_LessThanOrEqual( Left, Right )      True
' 
' Right: Decimal( 123.4553 )                     123.4553
' Decimal.op_Equality( Left, Right )             False
' Decimal.op_GreaterThan( Left, Right )          True
' Decimal.op_GreaterThanOrEqual( Left, Right )   True
' Decimal.op_Inequality( Left, Right )           True
' Decimal.op_LessThan( Left, Right )             False
' Decimal.op_LessThanOrEqual( Left, Right )      False
' 
' Right: Decimal( 123456000, 0, 0, False, 6 )    123.456000
' Decimal.op_Equality( Left, Right )             True
' Decimal.op_GreaterThan( Left, Right )          False
' Decimal.op_GreaterThanOrEqual( Left, Right )   True
' Decimal.op_Inequality( Left, Right )           False
' Decimal.op_LessThan( Left, Right )             False
' Decimal.op_LessThanOrEqual( Left, Right )      True
// Example of the decimal relational operators.
using System;

class Example
{
   const string dataFmt = "{0,43}    {1}";

   // Compare decimal parameters, and display them with the results.
   static void CompareDecimals(System.Windows.Controls.TextBlock outputBlock, decimal Left, decimal Right,
       string RightText)
   {
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(dataFmt, "Right: " + RightText, Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left == Right", Left == Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left >  Right", Left > Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left >= Right", Left >= Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left != Right", Left != Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left <  Right", Left < Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left <= Right", Left <= Right) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      decimal Left = new decimal(123.456);

      outputBlock.Text += 
          "This example of the decimal relational operators " +
          "generates the \nfollowing output. It creates several " +
          "different decimal values \nand compares them with " +
          "the following reference value.\n" + "\n";
      outputBlock.Text += String.Format(dataFmt,
          "Left: decimal( 123.456 )", Left) + "\n";

      // Create objects to compare with a 2-hour decimal.
      CompareDecimals(outputBlock, Left, new decimal(1.23456E+2),
          "decimal( 1.23456E+2 )");
      CompareDecimals(outputBlock, Left, new decimal(123.4567),
          "decimal( 123.4567 )");
      CompareDecimals(outputBlock, Left, new decimal(123.4553),
          "decimal( 123.4553 )");
      CompareDecimals(outputBlock, Left, new decimal(123456000, 0, 0, false, 6),
          "decimal( 123456000, 0, 0, false, 6 )");
   }
}

/*
This example of the decimal relational operators generates the
following output. It creates several different decimal values
and compares them with the following reference value.

                   Left: decimal( 123.456 )    123.456

               Right: decimal( 1.23456E+2 )    123.456
                              Left == Right    True
                              Left >  Right    False
                              Left >= Right    True
                              Left != Right    False
                              Left <  Right    False
                              Left <= Right    True

                 Right: decimal( 123.4567 )    123.4567
                              Left == Right    False
                              Left >  Right    False
                              Left >= Right    False
                              Left != Right    True
                              Left <  Right    True
                              Left <= Right    True

                 Right: decimal( 123.4553 )    123.4553
                              Left == Right    False
                              Left >  Right    True
                              Left >= Right    True
                              Left != Right    True
                              Left <  Right    False
                              Left <= Right    False

Right: decimal( 123456000, 0, 0, false, 6 )    123.456000
                              Left == Right    True
                              Left >  Right    False
                              Left >= Right    True
                              Left != Right    False
                              Left <  Right    False
                              Left <= Right    True
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.