Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

Working with Operators

C# supports a wide variety of operators, but we only cover the more commonly used ones. An operator is a special symbol that indicates which operation to perform in an expression. All the C# predefined types support operators, although not all types support the same operators.

Table 3.8 shows all the C# operators in order of precedence. Within each category, operators have equal precedence.

Table 3.8. Operators and Operator Precedence in C#

Category

Operators

Primary

x.y f(x) a[x] x++ x-- new typeof checked unchecked ->

Unary

+ - ! ~ ++x --x (T)x true false & sizeof

Multiplicative

* / %

Additive

+ -

Shift

<< >>

Relational and Type Testing

< > <= >= is as

Equality

== !=

Logical AND

&

Logical XOR

^

Logical OR

¦

Conditional AND

&&

Conditional OR

¦¦

Conditional

?:

Assignment

= += -= *= /= %= &= ¦= ^= <<= >>=

Null-Coalescing

??

Lambda

=>

Arithmetic and Assignment Operators

You have already seen the assignment operator (=) in many of the previous examples. This operator simply stores the value of the right operand in the variable indicated by its left operand. Both operands must be the same type or the right operand must be implicitly convertible to the type of the left operand.

C# provides arithmetic operators that support the standard mathematical operations of addition (+), subtraction (), multiplication (*), and division (/). Subtle differences exist between the behavior of the C# arithmetic operators and the arithmetic rules you learned in school. In particular, integer division behaves a bit differently depending on the data types you are dividing. When dividing one integer by another, the result is an integer. Any remainder is discarded, and the result is rounded toward zero. To obtain the remainder of an integer division, you must use the modulus operator (%).

C# also supports a compound assignment operator, which combines an arithmetic operation and an assignment in a single operator. A corresponding operation (+=, –=, *=, /=) exists for each of the standard arithmetic operators and the modulus operator (%=), which combine addition, subtraction, multiplication, division, and modulus division with assignment.

For example, suppose you need to increment a variable by one. Using the standard arithmetic operators, such an action would typically look like this:

i = i + 1;

However, by using the addition compound assignment operator, this operation could be performed like this:

i += 1;

Relational Operators

The relational operators, shown in Table 3.9, are used when comparing two values and result in a Boolean value.

Table 3.9. Relational Operators in C#

Name

Operator

Expression

Result

Equals

==

x == 20y == 30

truefalse

Not equals

!=

x != 20y != 30

falsetrue

Greater than

>

x > yy > x

truefalse

Greater than or equals

>=

x >= yy >= x

truefalse

Less than

<

x < yy < x

falsetrue

Less than or equals

<=

x <= yy <= x

falsetrue

Assuming x = 20 and y = 10.

In many programming languages, the assignment and equality operators are easily confused because they use the same symbol. This confusion can result in accidental assignments, which remains one of the more common programming mistakes today. To help minimize the possibility for confusion, C# defines a different operator for equality (==).

Logical Operators

The logical operators, shown in Table 3.10, evaluate Boolean expressions that result in either true or false.

Table 3.10. Logical Operators in C#

Name

Operator

Expression

Result

And (Conditional)

&&

(x == 20) && (y == 30)

false; both expressions must be true

And (Logical)

&

(x == 20) & (y == 30)

false; both expressions must be true

Or (Conditional)

¦¦

(x == 20) ¦¦ (y == 30)

true; either or both expressions must be true

Or (Logical)

¦

(x == 20) ¦ (y == 30)

True; either or both expressions must be true

Or (Exclusive)

^

(x == 20) ^ (y == 30)

true; the expressions are both different

Not

!

!(x == 30)

true; the expression must be false

Assuming x = 20 and y = 10.

The rules for the logical operators can be easily summarized, assuming an x and y that are Boolean expressions, as shown in Table 3.11.

Table 3.11. Logical Operators Truth Table

X

Y

X && Y

X ¦¦ Y

X ^ Y

true

true

true

true

false

true

false

false

true

true

false

true

false

true

true

false

false

false

false

false

Conditional Operator

The conditional operator (also called a ternary operator, or ternary if, because it takes three terms) is useful for writing concise expressions and evaluates a condition returning one of two values depending on the result.

The conditional operator has the following form:

condition ? consequence : alternative

When condition is true, the consequence is evaluated and becomes the result. However, when condition is false, the alternative is evaluated and becomes the result instead.

  • + Share This
  • 🔖 Save To Your Account