- Operator
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

- Operator

The - operator can function as either a unary or a binary operator.

- expr
expr1 - expr2

Where:

expr
An expression.
expr1
An expression.
expr2
An expression.

Remarks

Unary - operators are predefined for all numeric types. The result of a unary - operation on a numeric type is the negation of the operand.

Binary - operators are predefined for all numeric and enumeration types to subtract the second operand from the first.

Delegate types also provide a binary - operator, which performs delegate removal.

User-defined types can overload the unary - and binary - operators (see operator).

Example

// cs_operator_minus.cs
using System;
class Test 
{
   public static void Main() 
   {
      int a = 5;
      Console.WriteLine(-a);
      Console.WriteLine(a - 1);
      Console.WriteLine(a - .5);
   }
}

Output

-5
4
4.5

See Also

C# Operators | 7.6.2 Unary minus operator | 7.7.5 Subtraction operator

Show:
© 2016 Microsoft