Chapter 3: Writing C# Expressions
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 chapter provides guidance in building C# expressions.
This chapter demonstrates expressions created with each of C#'s built-in operators. All aspects of operators are covered in order to provide an understanding of their effects.
There are four types of operatorsunary, binary, ternary, and a few others that don't fit into a category. Unary operators affect a single expression. Binary operators require two expressions to produce a result. The ternary operator has three expressions. The others can only be explained 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.
As previously stated, unary operators affect a single expression. In many instances, the unary operators enable operations with simpler syntax than a comparable binary operation. The unary operators include + (plus), - (minus), ++ (increment), -- (decrement), ! (logical negation), and ~ (bitwise complement).
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. Since 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:
int negative = -1; int positive = 1; int result; result = +negative; // result = -1 result = +positive; // result = 1
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 zero. 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:
int negInt = -1; decimal posDec = 1; float negFlt = -1.1f; double nanDbl = Double.NaN; int resInt; decimal resDec; float resFlt; double resDbl; resInt = -negInt; // resInt = 1 resDec = -posDec; // resDec = -1 resFlt = -negFlt; // resFlt = 1.1 resDbl = -nanDbl; // resDbl = NaN
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. Since the assignment occurs first, the value of index is placed into count, making it equal 6. Then index is incremented to become 7.
Here's an example of a pre-increment 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 pre-increment operator. Index is incremented before the assignment occurs. Since index is incremented first, its value becomes 7. Next, the assignment occurs to make the value of count equal 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. Since the assignment occurs first, the value of index is placed into count, making it equal 6. Then index is decremented to become 5.
Here's an example of a pre-decrement 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 pre-decrement operator. Index is decremented before the assignment occurs. Since 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:
bool bexpr = true; bool bresult = !bexpr; // bresult = false bresult = !bresult; // bresult = true
The Bitwise Complement Operator
A bitwise complement operator (~) inverts the binary representation of an expression. All 1 bits are turned to 0. Likewise, all 0 bits are turned to 1. Here's an example:
byte bitComp = 15; // bitComp = 15 = 00001111b byte bresult = (byte) ~bitComp; // bresult = 240 = 11110000b