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.

C# defines the following **arithmetic operators**:

Operator | Meaning |
---|---|

+ | Addition |

– | Subtraction (also unary minus) |

* | Multiplication |

% | Modulus |

/ | Division |

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:

Operator | Meaning |
---|---|

= = | 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:

Operator | Meaning |
---|---|

& | AND |

| | OR |

^ | XOR (exclusive OR) |

|| | Short-circuit OR |

&& | Short-circuit AND |

! | NOT |

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:

p | q | p & q | p | q | p ^ 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 |

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.

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:

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

Increments or decrements the value of a variable by one.

//prefix syntax ++variable --variable //postfix syntax variable++ variable--

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

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**:

Category | Operator | Associativity |
---|---|---|

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.

Recent Posts

C# - Generic Collection Namespace
C# - Generic List Class
C# - Path Class & Special Folders
C# - Regular Expressions Classes
C# - Directory & DirectoryInfo Class
C# - File & FileInfo Class
Features of C
Check Number Is Armstrong Number Or Not Using Function
Program To Check Number Is Palindrome Or Not Using Function
Program To Calculate Power Using Function

Popular Posts

Calculate Area And Perimeter Of Rectangle
C# - .NET Framework
Palindrome Number Series from 1 to 500
Program To Check Number Is Palindrome Or Not Using Function
C# Simple Interest Application Example
Calculating The Area and The Perimeter of Rectangle C# Program
Program To Find Power of a Number using for loop
Program To Calculate Power Using Function
C# Program To Print Muliplication Table of Number
C# - Home