Behind the Scenes Look at C#: Operators - The Unary Arithmetic Operators
(Page 3 of 8 )
Yes, there are two more arithmetic operators, but they are unary operators, not binary like the above operators. As we have seen, binary Operators work on two arguments, much like a method that has two parameters. The unary operators work on one argument only; it's very much like a method that has only one parameter. C# defines two unary arithmetic operators, the minus operator (-) and the plus operator (+). As usual, let's begin with an example:
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
int x = -10;
int y = +10;
int z = +-10;
int original = 3;
int result;
Console.WriteLine("x = {0}, y = {1}, z = {2}",x,y,z);
z = -10;
result = z * original;
Console.WriteLine("result = {0}", result);
result = z * +original;
Console.WriteLine("result = {0}", result);
Console.ReadLine();
}
}
}
The result will be as follows:

The minus operator, when associated with a variable (or a value) makes it a negative value to the C# compiler. The plus operator does nothing to the variable, so the variable is displayed in its original value -- which may be negative, as in our example.
Note the z variable, which contains the value +-10. This value actually is the same as -10, because if we say that we add nothing (+) to the value -10, it's still -10 with no modifications. The expression result = z * original; equal to 10 and the expression result = z * +original; is also equal to 10 because of that.
Next: Relational Operators >>
More C# Articles
More By Michael Youssef