In C#, symbols such as +, –, *, and / are called operators because they operate, or perform calculations, on one or more variables (operands) in an expression to produce the required results. Basically, C# operators provide syntax for the compiler on how to process data types within the calculation.
C# Operators can be separated into different categories. Some of them are listed below:
C# Operators are generally classified into three categories based on the number of arguments they can take:
- Unary ( ++ ) — increment operator that take single operand.
- Binary ( +,-,*, / ) — arithmetic operators that take two operands (left-associative).
- Ternary ( ? : ) — conditional operator that take three operands (right-associative)
C# Expressions
Expressions can be transformed by combining operators with one or more variables (operands) and literal values. You can create expressions to evaluate the result from simple or complicated statements.
int i = 4;
int j = (i * 4) + 3;
Console.WriteLine(j);
Output
19
C# Arithmetic Operators
C# arithmetic operators are the same ones you use on numeric values in math to perform mathematical operations such as addition, subtraction, multiplication, or division. Most of the operators in the below table are binary operators because they operate on two operands. C# uses infix notation for binary operators; the operator appears between the left and right operands. One thing to note is that all binary operators are left-associative, which means that operations are performed left to right except for the assignment and C# conditional operator (? :), which are performed right to left.
C# Arithmetic Operators
Operator | Description | Examples | |
+ | Add | Add two operands together. | a + b |
– | Subtract | Subtract the value of right-hand operand from left-hand operand. | a – b |
* | Multiply | Multiplies both operands. | a * b |
/ | Division | Divide the left-hand operand with the right-hand operand (numerator by de-numerator) | a / b |
% | Modulo | Divide the left-hand operand by the right operand and then returns the remainder. | a % b |
++ | Increment | Increase the value of the operand by 1. | a++ |
– – | Decrement | Decrease the value of the operand by 1. | a – – |
Note
- Integer division by 0 throws runtime exception DivideByZeroException.
- The + sign is used for both addition and string concatenation.
Increment and Decrement Operators (++
, --
)
Increment and decrement operators are considered unary, which means that they can only be used on one operand. You can also use them as a prefix or postfix operator depending on whether you want the variable to be updated before or after the expression is evaluated. For example:
int a = 3;
a++; // 3
--a; // Still 3
++a; // 4
int a = 0;
Console.WriteLine($"a is now {a}");
Console.WriteLine($"a++ : Outputs {a++}, a is now {a}");
Console.WriteLine($"++a : Outputs {++a}, a is now {a}");
Console.WriteLine($"--a : Outputs {--a}, a is now {a}");
Output
a is now 0
a++ : Outputs 0, a is now 1
++a : Outputs 2, a is now 2
–a : Outputs 1, a is now 1
Modulus (%
)
This name comes from the term modulo used in a branch of mathematics known as modular arithmetic. Modulo is the remainder after division. So, the % operator gives us the remainder, as if a division has taken place.
The result a % b is evaluated as a – (a / b) * b using integer operations. If b is zero, a DivideByZeroException is thrown. This is not used with floating-point numbers because you have no remainder after the division of floating-point values. For example:
int a = 13 % 3;
int b = 30 % 7;
Console.WriteLine($"remainder : {a}");
Console.WriteLine($"remainder : {b}");
Output
remainder : 1
remainder : 2
int a = 30;
int b = 7;
int c = a - (a / b) * b;
Console.WriteLine($"a – (a / b) * b : remainder is {c}");
Console.WriteLine($"a % b : remainder is {a % b}");
Output
a – (a / b) * b : remainder is 2
a % b : remainder is 2
More Examples
int addition = 20 + 10;
int subtraction = 20 - 10;
int multiplication = 20 * 10;
int division = 20 / 10;
int modulus_1 = 7 % 3;
double modulus_2 = 14.9 % 3.9;
Console.WriteLine($"addition: {addition}");
Console.WriteLine($"subtraction: {subtraction}");
Console.WriteLine($"multiplication: {multiplication}");
Console.WriteLine($"division: {division}");
Console.WriteLine($"modulus_1: {modulus_1}");
Console.WriteLine($"modulus_2: {modulus_2}");
Output
addition: 30
subtraction: 10
multiplication: 200
division: 2
modulus_1: 1
modulus_2: 3.2
C# Relational Operators
C# relational operators, also called comparison operators, are used to compare two or more values (operands) and always return a Boolean type. That is, the result of the comparison is either True or False. Because of this, they are good when used for decision making.
C# Relational Operators
Operator | Description | Examples | |
> | Greater than | Return true, if operand on the left is greater than the operand on the right. Otherwise, return false. | a > b |
< | Less than | Return true, if operand on the left side is less than the operand on the right side. Otherwise, return false. | a < b |
== | Equal to | Return true, if operand on the left side is equal to the operand on the right side. Otherwise, return false. | a == b |
!= | Not equal to | Return true, if operand on the left side is not equal to the operand on the right side. Otherwise, return false. | a != b |
>= | Greater than or equal to | Return true, if operand on the left side is greater than, or equal to, the operand on the right side. Otherwise, return false. | a >= b |
<= | Less than or equal to | Return true, if operand on the left side is less than or equal to the operand on the right side. Otherwise, return false. | a <= b |
Examples
Suppose variable a has a value of 1, and variable b has a value of 2:
int a = 1;
int b = 2;
// True if a is equal to b
Console.WriteLine($"a == b : {a == b}");
// True if a is not equal to b
Console.WriteLine($"a != b : {a != b }");
// True if a is less than b
Console.WriteLine($"a < b : {a < b}");
// True if a is less than or equal to b
Console.WriteLine($"a <= b : {a <= b}");
// True if a is greater than b
Console.WriteLine($"a > b : {a > b}");
// True if a is greater than or equal to b
Console.WriteLine($"a >= b : {a >= b}");
Output
a == b : False
a != b : True
a < b : True
a <= b : True
a > b : False
a >= b : False
C# Logical Operators
C# logical operators, also called C# Boolean operators, accept two bool values and return a Boolean result (True/False). This enables you to form more complex conditions by combining simple conditions. Some logical operators supported by C# are described in the following table.
C# Boolean Logical Operators
Operator | Description | Examples | |
&& | Conditional AND | Condition becomes true if both operands are true. Otherwise, return false. | (a && a) |
|| | Conditional OR | Condition becomes true if at least one of your operands is true. Otherwise, return false. | (a || b) |
! | Logical NOT (Negation) | Reverse the value of your Boolean variable, meaning If the condition is true then the operator will make it false and vice versa. | !(a && b) |
^ | Logical XOR | Returns true if one of the two operands has a true value. It also returns true if the operands have different values. | (a ^ b) |
Note: || and && are referred to as short-circuit operators.
Possible Combinations for &&
(Logical AND
) Operator
Console.WriteLine($"false && false: {false && false}");
Console.WriteLine($"false && true: {false && true}");
Console.WriteLine($"true && false: {true && false}");
Console.WriteLine($"true && true: {true && true}");
Output
false && false
: Falsefalse && true
: Falsetrue && false
: Falsetrue && true
: True
Possible Combinations for ||
(Logical OR
) Operator
Console.WriteLine($"false || false: {false || false}");
Console.WriteLine($"false || true: {false || true}");
Console.WriteLine($"true || false: {true || false}");
Console.WriteLine($"true || true: {true || true}");
Output
false || false
: Falsefalse || true
: Truetrue || false
: Truetrue || true
: True
Possible Combinations for !
(Logical NOT
) Operator
Console.WriteLine($"!false: {!false}");
Console.WriteLine($"!true: {!true}");
Output
!false
: True!true
: False
Possible Combinations for ^
(Exclusive OR
) Operator
Console.WriteLine($"false ^ false: {false ^ false}");
Console.WriteLine($"false ^ true: {false ^ true}");
Console.WriteLine($"true ^ false: {true ^ false}");
Console.WriteLine($"true ^ true: {true ^ true}");
Output
false ^ false
: Falsefalse ^ true
: Truetrue ^ false
: Truetrue ^ true
: False
Note: The && operator avoids evaluating both expressions unnecessarily.
The part of an expression containing the && operator is evaluated only until it’s known whether the condition is true or false. Thus, evaluation of the expression (UserName == “Admin” && password == “Admin123”) stops immediately if UserName is not equal to “Admin” and continues only if UserName is equal to “Admin” (meaning true). Then C# evaluates password == “Admin123”. The if statement considers the combined condition that is true if and only if both conditions are true. The && operator uses a short-circuit evaluation because it short-circuits around the second Boolean expression, if necessary.
C# Assignment Operators
C# assignment operators are used to assign a new value to a variable. This operator simply assigns whatever value is on the right to the variable on the left. Make sure that the data type of the right-hand operand is the same as the data type of the left-hand operand.
The = operator is called the simple assignment operator. It assigns the value of the right operand to the variable on the left side of the assignment.
The compound assignment operator combines assignment with another operator to perform operations. The compound operators are the following:
+= -= *= /= %= &= |= ^= <<= >>=
Syntax
Compound-assignment expression such as
expression1 += expression2
is equivalent to
expression1 = expression1 + expression2
Commonly used Assignment Operators in C#
Operator | Description | Examples | |
= | Assign | Assign values to the left operand | a = 6 |
+= | Plus AND assign | Adds right and left operand and assign the result to the left operand | a += 6 means a = a + 6 |
-= | Minus AND assign | Subtracts right and left operand and assign the result to the left operand | a -= 6 means a = a – 6 |
*= | Multiply AND assign | Multiplies right and left operand and assign the result to the left operand | a *= 6 means a = a * 6 |
/= | Divide AND assign | Divides right and left operand and assign the result to the left operand | a /= 6 means a = a / 6 |
%= | Modulus AND assign | Modulus on two operands and assign the result to the left operand | a %= 6 means a = a % 6 |
Examples
int a, b, c;
// Syntax to initialize multiple values
a = b = c = 6;
Console.WriteLine(a += 1); // means a=a+1;
Console.WriteLine(a -= 2); // means a=a-2;
Console.WriteLine(a *= 3); // means a=a*3;
Console.WriteLine(a /= 3); // means a=a/3;
Console.WriteLine(a %= 3); // means a=a%3;
Output
7
5
15
5
2
C# Reference | Microsoft Docs
- C# Operators