Operators

Many of the C# operators are supported by the built-in types, and allow you to perform basic operations with values of those types. C# objects like DateTime, and TimeSpan (which you met in the previous section) must be programmed with behaviour that is appropriate to a specific operator

The operators can be grouped as follows

Arithmetic operators

Are divided into two groups, binary and unary operators.

Binary operators being +, - , /, *, and %,

Always used as follows - <left operand> <operator> <right operand>

Expression

Result

Expression

Result

3 + 4

7

var1 - 3.142 * 2

Assuming var1 has a value 10, the result would be 3.716

But what if we wanted to subtract 3.142 from var1 first then multiply the result by 2? C# arithmetic operators obey the laws of precedence. *, /, and % always execute before + and - operators. We can change the precedence using parenthesis.

Expression

Result

Expression

Result

(var1 - 3.142) * 2

Assuming var1 has a value of 10, the result would be 13.716

Unary operators being ++, --, -, !, ~, sizeof, and (type)*

For ++ and – (the increment and decrement operators; respectively)

Can be used as follows

Usage

Result

Usage

Result

<variable>++

Will print

10

11

total = counter++ first assigns the value in counter to total (this will be 10), it will then increment counter by 1

++<variable>

Will print

11

11

total = ++counter first increments the value in counter (this will be 10) by one, it then assigns the value in counter to total

<variable>--

Will print

10

9

total = counter-- first assigns the value in counter to total (this will be 10), it will then decrement counter by 1

--<variable>

Will print

9

9

total = --counter first decrements the value in counter (this will be 10) by one, it then assigns the value in counter to total

-<variable>

Will print

10

-10

Unary minus can be used as follows

Usage

Result

Usage

Result

-<variable> or -<value>

Will print

10

-10

-( -<variable>)) or -( -<value>))

Will print

10

-10

10

Comparison operators

These are < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal) comparisons. They may be used with char, integral, and floating point types.

These operators always result in a TRUE or FALSE.

These operators may be used as follows

Usage

Result

Usage

Result

Will print

Sum is less than 10

But we know that is incorrect

Will print

Sum is greater than 10

But we know this is incorrect

In the two examples above, both logic expressions could be valid. The error is in the output statements.

Think carefully about the intended resultant logic after each logic expression

Boolean logical operators

Unary logical operator

NOT (logical negation) operator, used to reverse a logic value. It produces true if the value is false, and false if the value is true.

Usage

Result

Usage

Result

!variable or !<value>

Will print

true

false

Binary logical operators

The binary logical operators have this form: <left operand> <logical operator> <right operand>

The left and right hand operands must be boolean values. Boolean values can be direct boolean values or the result of an expression that results in a boolean value

These operators always result in a TRUE or FALSE.

Usage

Result

Usage

Result

Will print

lh & rh are false

The && (AND) operator can only result in true if both the lh and rh are true

 

Will print

Neither lh & rh are true

The && operator is known as a short circuit operator. If the lh is false, then the rh expression is not evaluated

Will print

Some other method

Neither lh & rh are true

A single & means that both lh and rh expressions will be evaluated even if the lh is false

Will print

Either lh or rh are true

The || (OR) operator will result in true if any of the following conditions are met

 

Will print

Either lh or rh are true

The || operator is known as a short circuit operator. If the lh is true, then the rh expression is not evaluated

Will print

Some other method

Either lh & rh are true

A single | means that both lh and rh expressions will be evaluated even if the lh is true

Equality operators

The == (equality) and != (inequality) operators check the left and right hand operands are equal or not equal, respectively

The == operator returns true if the lh and rh operands equal.

The != operator returns true if the lh and rh operands are NOT equal.

These operators always result in a TRUE or FALSE.

Working with built-in types

Usage

Result

Usage

Result

Will print

The lh and rh are equal

 

Will print

The lh and rh are NOT equal

String Equality

Two strings are considered to be equal when they are both null, or of the same length and contain identical characters (their ASCII encodings are the same)

Usage

Result

Usage

Result

Will print

The lh and rh are equal

 

Will print

The lh and rh are NOT equal