From the author of

From the author of

C# provides a complete set of language elements for writing expressions. An
expression is a set of language elements combined to perform a meaningful computation.
This article provides guidance on building C# expressions.

This article also demonstrates expressions created with each of C#'s built-in
operators. All aspects of operators are covered to provide an understanding
of their effects.

There are four types of operators—unary, binary, ternary, and a few others
that don't fit into a category. Unary operators effect a single expression.
Binary operators require two expressions to produce a result. The ternary operator
has three expressions. The others can be explained only by reading each of their
descriptions.

For C++ and Java Programmers

C# operators and their precedence are the same. No surprises here at all.
If desired, you could skip this section without missing anything.

Mathematical operations on floating-point types are performed according to
IEEE 754 arithmetic.

The Plus Operator

The plus operator (+) has no effect on the expression it's used
with. Why would a language have an operator that has no effect? For consistency.
Most C# operators have a logical complement. Because there is a minus operator,
its logical complement is the plus operator. The + operator is
available to explicitly document code. Here are a couple examples:

The Minus Operator

The minus operator (-) allows negation of a variable's value.
In integer and decimal types, the result is the number subtracted from 0. For
floating-point types, the - operator inverts the sign of the number.
When a value is NaN (not a number), the result is still NaN.
Here are some examples:

The Increment Operator

The increment operator (++) allows incrementing the value of a
variable by 1. The timing of the effect of this operator depends upon which side
of the expression it's on.

Here's a post-increment example:

int count;
int index = 6;
count = index++; // count = 6, index = 7

In this example, the ++ operator comes after the expression
index. That's why it's called a post-increment operator. The
assignment takes place, and then index is incremented. Because the
assignment occurs first, the value of index is placed into
count, making it equal to 6. Then index is
incremented to become 7.

Here's an example of a preincrement operator:

int count;
int index = 6;
count = ++index; // count = 7, index = 7

This time, the ++ operator comes before the expression
index. This is why it's called the preincrement operator.
index is incremented before the assignment occurs. Because
index is incremented first, its value becomes 7. Next, the
assignment occurs to make the value of count equal to 7.

The Decrement Operator

The decrement operator (--) allows decrementing the value of a
variable. The timing of the effect of this operator again depends upon which
side of the expression it is on. Here's a post-decrement example:

int count;
int index = 6;
count = index--; // count = 6, index = 5

In this example, the -- operator comes after the expression
index, and that's why it's called a post-decrement operator.
The assignment takes place, and then index is decremented. Because the
assignment occurs first, the value of index is placed into
count, making it equal to 6. Then index is
decremented to become 5.

Here's an example of a predecrement operator:

int count;
int index = 6;
count = --index; // count = 5, index = 5

This time the -- operator comes before the expression
index, which is why it's called the predecrement operator.
index is decremented before the assignment occurs. Because
index is decremented first, its value becomes 5, and then the
assignment occurs to make the value of count equal 5.

The Logical Complement Operator

A logical complement operator (!) serves to invert the result of a
Boolean expression. The Boolean expression evaluating to true will be
false. Likewise, the Boolean expression evaluating to false will be
true. Here are a couple examples: