Share via


Decimal.Addition Operator

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

Adds two specified Decimal values.

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

Syntax

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

Parameters

Return Value

Type: System.Decimal
The Decimal result of adding d1 and d2.

Exceptions

Exception Condition
OverflowException

The return value is less than MinValue or greater than MaxValue.

Examples

The following code example creates several pairs of Decimal values and calculates their sums with the Addition operator.

' Example of the Decimal addition and subtraction operators.

Module Example

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

   ' Display Decimal parameters and their sum and difference.
   Sub ShowDecimalSumDiff(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As Decimal, ByVal Right As Decimal)

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Decimal Left", Left) & vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Decimal Right", Right) & vbCrLf

      ' The op_Addition and op_Subtraction operators must be
      ' explicitely coded in Visual Basic. If binary + or - are 
      ' used, other methods are called.
      outputBlock.Text += String.Format(dataFmt, _
          "Decimal.op_Addition( Left, Right )", _
          Decimal.op_Addition(Left, Right)) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, _
          "Decimal.op_Subtraction( Left, Right )", _
          Decimal.op_Subtraction(Left, Right)) + vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += _
          "This example of the Decimal addition and " & _
          "subtraction operators " & vbCrLf & "generates the " & _
          "following output. It displays the sum and " & vbCrLf & _
          "difference of several pairs of Decimal objects." + vbCrLf

      ' Create pairs of Decimal objects.
      ShowDecimalSumDiff(outputBlock, _
          New Decimal(1230000000, 0, 0, False, 7), _
          0.00123D)
      ShowDecimalSumDiff(outputBlock, 123.456789D, 0.006789D)
      ShowDecimalSumDiff(outputBlock, 12345678900000000D, _
          0.00000000123456789D)
      ShowDecimalSumDiff(outputBlock, 123456789.0123456789D, _
          123456789.1123456789D)
   End Sub
End Module

' This example of the Decimal addition and subtraction operators
' generates the following output. It displays the sum and
' difference of several pairs of Decimal objects.
' 
' Decimal Left                                              123.0000000
' Decimal Right                                                 0.00123
' Decimal.op_Addition( Left, Right )                        123.0012300
' Decimal.op_Subtraction( Left, Right )                     122.9987700
' 
' Decimal Left                                               123.456789
' Decimal Right                                                0.006789
' Decimal.op_Addition( Left, Right )                         123.463578
' Decimal.op_Subtraction( Left, Right )                      123.450000
' 
' Decimal Left                                        12345678900000000
' Decimal Right                                     0.00000000123456789
' Decimal.op_Addition( Left, Right )     12345678900000000.000000001235
' Decimal.op_Subtraction( Left, Right )  12345678899999999.999999998765
' 
' Decimal Left                                     123456789.0123456789
' Decimal Right                                    123456789.1123456789
' Decimal.op_Addition( Left, Right )               246913578.1246913578
' Decimal.op_Subtraction( Left, Right )                   -0.1000000000
// Example of the decimal addition and subtraction operators.
using System;

class Example
{
   const string dataFmt = "   {0,-18}{1,31}";

   // Display decimal parameters and their sum and difference.
   static void ShowDecimalSumDiff(System.Windows.Controls.TextBlock outputBlock, decimal Left, decimal Right)
   {
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal Left", Left) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal Right", 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)
   {
      outputBlock.Text += "This example of the decimal " +
          "addition and subtraction operators \ngenerates the " +
          "following output. It displays the sum and \n" +
          "difference of several pairs of decimal objects." + "\n";

      // Create pairs of decimal objects.
      ShowDecimalSumDiff(outputBlock, 
          new decimal(1230000000, 0, 0, false, 7),
          0.0012300M);
      ShowDecimalSumDiff(outputBlock, 123.456789M, 0.006789M);
      ShowDecimalSumDiff(outputBlock, 12345678900000000M,
          0.00000000123456789M);
      ShowDecimalSumDiff(outputBlock, 123456789.0123456789M,
          123456789.1123456789M);
   }
}

/*
This example of the decimal addition and subtraction operators
generates the following output. It displays the sum and
difference of several pairs of decimal objects.

   decimal Left                          123.0000000
   decimal Right                           0.0012300
   Left + Right                          123.0012300
   Left - Right                          122.9987700

   decimal Left                           123.456789
   decimal Right                            0.006789
   Left + Right                           123.463578
   Left - Right                           123.450000

   decimal Left                    12345678900000000
   decimal Right                 0.00000000123456789
   Left + Right       12345678900000000.000000001235
   Left - Right       12345678899999999.999999998765

   decimal Left                 123456789.0123456789
   decimal Right                123456789.1123456789
   Left + Right                 246913578.1246913578
   Left - Right                        -0.1000000000
*/

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.