In this article, we are going to talk about operators in C#, how to use them, and what are the differences between each of them.
For the complete navigation of this series check out: C# Back to Basics.
In this article, we are going to cover:
- Type of Operators in C#
- Arithmetic Operators in C#
- Relational Operators in C#
- Logical Operators in C#
- Increment and Decrement Operators in C#
- Conclusion
Type of Operators in C#
The most used operators in C# are:
Arithmetic Operators in C#
Arithmetic operators are defined for all the numeric data types. Operators +, -, *, / represent the basic binary arithmetic operations (addition, subtraction, multiplication and division). Operator % is the remainder after division.
The important thing to notice is that the + operator behaves differently with the number and string types. With the numbers, the result of expression 5 + 5 is 10. But with the strings, the result of expression β5β + β5β is β55β. So with the number type, it is an addition operator but with the string type, it is a concatenation operator.
Relational Operators in C#
All the relational operators return a true or false result. They are used to compare expressions or variables from both sides of the relational operator. These operators have a lower priority than arithmetic ones. So in the following example: x*a-8*b>y+5*z;
the left side of the greater than operator has calculated first then the right side and then they are compared.
For the value-type variables and the strings, the ==
(equality) operator will return true only if they are the same, otherwise, it will return false. But if variables are reference types then the ==
operator will return true only if those two variables point to the same memory location, otherwise, it will return false.
So let’s see this through an example:
class Program { static void Main(string[] args) { int a = 15; int b = 15; string s1 = "This is a string"; string s2 = "This is a string"; Student student1 = new Student("John", 25); Student student2 = new Student("John", 25); } }
If we place a breakpoint in our code and inspect the result using the Watch window:
We see that a
and b
are the equal as well as the s1
and s2
. But the student1
and student2
are not equal because they point to different memory locations. But if we create another variable of type Student
and assign the value of student1
variable to it, the ==
operator will return true:
Student student1 = new Student("John", 25); Student student2 = new Student("John", 25); Student student3 = student1;
Logical Operators
The logical operators &&
(and) and ||
(or) serve to connect logical values. Expression <expression1>&&<expression2>
is true only if both expressions are true. Expression <expression1>||<expression2>
is false only if both expressions are false, otherwise, it is true.
The !
(negation) operator negates the logical value it is applied on. It has the highest priority of all the operators mentioned. So the expression !logicalValue
will be false only if logicalValue is true and vice versa.
Letβs see this with an example:
class Program { static void Main(string[] args) { int a = 14; int b = 30; int c = 20; if(a < b && a < c) { Console.WriteLine($"min number is {a}"); } if(a < b || a < c) { Console.WriteLine("The a variable is smaller then b or c"); } if(!(a > b)) { Console.WriteLine("a is less than b"); } } }
Increment and Decrement Operators
In the C# language, we can use operators that increment and decrement the variable value by 1. Those operators are ++
and --
, and they are very useful in many cases. So, the better way of writing this code:
static void Main(string[] args) { int a = 15; a = a + 1; //now it is 16 }
Would be:
static void Main(string[] args) { int a = 15; a++; //now it is 16 }
The same applies to the --
operator.
These two operators have the prefix notations: --variable
, ++variable
and the suffix notations: variable--
, variable++
. Even though both notations will change the value by 1, the result will be different. This is easier to explain through an example:
class Program { static void Main(string[] args) { int a = 15; int b = --a; int c = 20; int d = c--; } }
What we can notice is that the prefix notation decrements the value of a
variable first and then assigns that value to the b
variable. But the expression with suffix notation is different. The value of the c
variable is assigned to the d
variable first and then decremented by 1.
The same applies to the increment operator:
class Program { static void Main(string[] args) { int a = 15; int b = ++a; int c = 20; int d = c++; } }
Conclusion
Excellent. Now we have more knowledge about operators in C#.
InΒ the next post, we are going to talk about type conversions in C#.