C# -


C# provides an extensive set of operators that give the programmer detailed control over the construction and evaluation of expressions. Most of C#’s operators fall into the following categories: arithmetic, bitwise, relational, and logical.

Arithmetic Operators

C# defines the following arithmetic operators:

+ Addition
Subtraction (also unary minus)
* Multiplication
% Modulus
/ Division

Relational and Logical Operators

In the terms relational operator and logical operator, relational refers to the relationships that values can have with one another, and logical refers to the ways in which true and false values can be connected together. Since the relational operators produce true or false results, they often work with the logical operators.

The relational operators are as follows:

= = Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

The logical operators are as follows:

| OR
^ XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND

For the logical operators, the operands must be of type bool, and the result of a logical operation is of type bool. The logical operators, &, |, ^, and !, support the basic logical operations AND, OR, XOR, and NOT, according to the following truth table:

pqp & qp | qp ^ q!p
False False False False False True
True False False True True False
False True False True True True
True True True True False False
Short-Circuit Logical Operators

C# supplies special short-circuit versions of its AND and OR logical operators that can be used to produce more efficient code. To understand why, consider the following. In an AND operation, if the first operand is false, then the outcome is false no matter what value the second operand has. In an OR operation, if the first operand is true, then the outcome of the operation is true no matter what the value of the second operand. Thus, in these two cases there is no need to evaluate the second operand. By not evaluating the second operand, time is saved and more efficient code is produced.

Compound assignment operators

The compound assignment operators consist of two operators. They are shorthand operator

    a = a + 3;
    a += 3;

The += compound operator is one of these shorthand operators. The above two expressions are equal. Value 3 is added to the a variable.

Other compound operators are:

-= *= /= %= &= |= <<= >>=

Increment (++) and Decrement (--) Operators

Increments or decrements the value of a variable by one.

//prefix syntax
//postfix syntax

The C# ?? null coalescing operator

One of the subtle (but cool) language features of C# is the ?? "null coalescing" operator. This provides a nice, terse way to check whether a value is null, and if so return an alternate value.

If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error

    int? x = null;//x is nullable value type
int z = 0;//z is non-nullable value type

But if you use ?? operator. You won't have any problem.

z = x ?? 1;//with ?? operator No issues 

Operator precedence and associativity

When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator. The precedence of an operator is established by the definition of its associated grammar production. For example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or - operators, thus giving the + and - operators lower precedence than the *, /, and % operators.

The following table summarizes all operators in order of precedence from highest to lowest:

Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed:

  • Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example, x + y + z is evaluated as (x + y) + z.
  • The assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z).

Precedence and associativity can be controlled using parentheses. For example, x + y * z first multiplies y by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z.