In this post, we are gonna learn about Operators in C#.
Operators in C#
- The operator is a special symbol that specifies which operations you will perform on the values of various data types.
- C# provides many operators type: Arithmetic, comparison, logical and more.
- C# has usually had one or two operands.
- The operator has different meanings depending on the data type, ex the + operator with numbers mean add but with strings mean concatenation.
Operators Type
We will list the different operator's types that used mostly in c#,
Arithmetic operators
There is one of the built-in operators used in C# with numbers to perform mathematical operations.
+ Operator (Addition)
Used to carry out the addition operation " add value to another value".
Ex:
int x = 5;
int y = 6;
int z = x + y;
Console.WriteLine(z);// the output will be 11
- Operator (Subtraction)
Used to subtract a value from another value.
Ex:
int x = 5;
int y = 6;
int z = y-x;
Console.WriteLine(z);// the output will be 1
* Operator (Multiplication)
Used to multiply two values.
Ex:
int x = 5;
int y = 6;
int z = y*x;
Console.WriteLine(z);// the output will be 30
/ Operator (Division)
Used to divide two values.
Ex:
int x = 30;
int y = 6;
int z = x/y;
Console.WriteLine(z);// the output will be 5
% Operator (Module)
Return the division remainder.
Ex:
int x = 5;
int y = 2;
int z = x%y;
Console.WriteLine(z);// the output will be 1
Assignment operators
Below, we're gonna list some of the most used assignment operations:
"= " The assignment operator
Assigns the value of its right-hand operand to a variable.
Ex:
int x=2;
Console.WriteLine(x);// the output will be 2
+= The addition assignment operator
Add a value to a variable in the same line.
Ex:
int x=2;
x+=1;// means x=x+1
Console.WriteLine(x);// the output will be 3
- = The subtraction assignment operator
Subtract a value to a variable in the same line.
Ex:
int x=2;
x-=1;// means x=x-1
Console.WriteLine(x);// the output will be 1
*= Operator
Multiply a value to a variable in the same line.
Ex:
int x=2;
x*=2; // means x=x*2
Console.WriteLine(x);// the output will be 4
/= Operator
Divide a value to a variable in the same line.
Ex:
int x=2;
x/=2; // means x=x/2
Console.WriteLine(x);// the output will be 1
%= Operator
Ex:
int x=5;
x%=3; // means x=x%3
Console.WriteLine(x);// the output will be 2
Logical Operators
Logical operations used to perform logical operations such as and operation "&", or operation "|" and the "!" negation operation.
"&" and Operator:
- It gives true only if the two operands is true.
- If one of the operands is false, the AND operator will evaluate it to false
Ex:
int x=5;
Console.WriteLine(x>2 && x<6);// the output will be true
"|" or Operator:
- It gives true if one of the two operands at least is true.
- If the two operands are false, the OR operator will evaluate it to false.
Ex:
int x=5;
Console.WriteLine(x>2 || x<4);// the output will be true one condition is true x>2
"!" negation Operator "not":
- it reverses the result, it gives true if the result is false and false if the result is true.
Ex:
int x=5;
Console.WriteLine(!(x>2 || x<4));// the output will be false
Comparison Operators
Comparison operators used to compare two values.
"==" Equal to:
Ex:
int x=5;
int y=6;
Console.WriteLine(x == y);// the output will be false
"!=" Not Equal to:
Ex:
int x=5;
int y=6;
Console.WriteLine(x != y);// the output will be true
">" Greater than and "<" less than
Ex:
int x=5;
int y=6;
Console.WriteLine(x > y);// the output will be false
Console.WriteLine(x<y);// the output will be true
">=" Greater than or Equal and "<=" Less than or Equal
Ex:
int x=5;
int y=5;
Console.WriteLine(x >= y);// the output will be true
Console.WriteLine(x <= y);// the output will be true
"+" Unary Plus
Leaves the sign of the operand as it without no change
Ex:
int number=10;
Console.WriteLine(+number) //the output will be 10
"-" Unary minus
Leaves the sign of the operand as it without no change
Ex:
int number=10;
Console.WriteLine(-number) //the output will be -10
"++" Increment operator
Increment the value of the operand with one
Ex:
int number=10;
Console.WriteLine(++number) //the output will be 11
"--" Decrement operator
Decrement the value of the operand with one.
Ex:
int number=10;
Console.WriteLine(--number) //the output will be 9
Operator precedence
Operators with higher precedence are evaluated before those with lower precedence.
Ex:
In this example the precedence of * is higher than the precedence of + then it will perform first the * operator then the + operator.
var a = 2 + 2 * 2;
Console.WriteLine(a);//the output will be 6
Conclusion
In this post, we have explored the different operators in C#.
See Also