![]() |
VOOZH | about |
In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.
C# has some set of operators that can be classified into various categories based on their functionality. Categorized into the following types:
Arithmetic operators are used to perform basic mathematical operations on numeric values.
Example:
Addition: 12 Subtraction: 4 Multiplication: 32 Division: 2 Modulo: 0
Relational operators are used to compare values. And we get the answer in either true or false ( boolean). Let's learn about different relation operators.
Example:
False True False True False True
Used when multiple conditions and there we can combine these to compare complex conditions.
Example:
Either a or b is true b is not true
Assignment operators are used to assign values to variables. The assignment operator is combined with others to create shorthand compound statements. Common compound operators include:
Example:
Add Assignment: 15 Subtract Assignment: 12 Multiply Assignment: 24 Division Assignment: 6 Modulo Assignment: 1
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
++ (Increments by 1)
-- (Decrements by 1)
Example:
++a returns: 6 a++ returns: 6 Final value of a: 7 --a returns: 6 a-- returns: 6 Final value of a: 5
Bitwise operators are used to perform bit-level operations on integer values. It takes less time because it directly works on the bits.
Example:
2 10 8 -11 20 5
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false.
condition ? if true : if false
Example:
a is greater
The null-coalescing operator (??) is used to provide a default value when a variable is null.
Example:
Default Name
The following table summarizes the associativity of various operators in C#:
Operator Type | Operators | Associativity |
|---|---|---|
Postfix Operators | ++, -- | Left to Right |
Unary Operators | + , - , ! | Right to Left |
Multiplicative Operators | * , / , % | Left to Right |
Additive Operators | + , - | Left to Right |
Shift Operators | << , >> | Left to Right |
Relational Operators | < , > , <= , >= | Left to Right |
Equality Operators | ==, != | Left to Right |
Bitwise operator | & , | , ^ | Left to Right |
Logical Operator | && , || | Left to Right |
Conditional Operator | ?: | Right to Left |
Assignment Operators | =, +=, -=, *=, /=, %=, ... | Right to Left |
Operator precedence is the most important to evaluate any expression because it gives us an idea about how the different operation performs and which one has the higher precedence.
The below table shows the precedence of different operators in C# in ascending order.
Operator | Description |
|---|---|
(), [], . | Parentheses, Array indexing, Member access |
++, --, !, ~ | Unary increment/decrement, logical NOT, bitwise NOT |
*, /, % | Multiplication, Division, Modulus |
+, - | Addition, Subtraction |
==, !=, >, < | Relational operators |
Example:
Ans of a + b * c: 34