Working with operators is an important part of learning how to program. Every programming language uses operators. You will need to use them in essentially every program you write. Therefore every programmer will have to learn how to use them.
In this post I will write about operators in C# and show examples of how to work with some of them.
What is an operator?
An operator is a special character that allows you to process and perform different mathematical or logical operations on data types and objects. Some examples of operators are the same signs that are used in every day math (+, -, *, /). These are called arithmetic operators. There are several different categories and types of operators in C#.
Types of operators
Here are the different types of operators used in C#:
|Arithmetic||Performs mathematical operations.||-, +, *, /, %, ++, --|
|Logical||Evaluates Boolean expressions that result in true or false.||&&, ||, !, ^|
|Bitwise||Performs operations on binary numbers.||&, |, ^, ~, <<, >>|
|Comparison||Compares two operands.||==, !=, >, <, >=, <=|
|Assignment||Assigns values to variables.||=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=|
|Type conversion||Converts data from one type to another.||(type), as, is, typeof, sizeof|
|Other||Other operators.||., new, (), , ?:, ??|
Number of operands
Operators are also classified into different types based on the number of operands that they can take.
Operators that use one operand are called unary operators.
Operators that use two operands are called binary operators.
The conditional operator ?: takes three operands. It’s also called the ternary operator.
Example of using operators
int x = 10; int y = 2; Console.WriteLine(x + y); // Output: 12 Console.WriteLine(x - y); // Output: 8 Console.WriteLine(x / y); // Output: 5 x = x - 5; Console.WriteLine(x); // Output: 5 Console.WriteLine(x * 5); // Output: 25 y = 5 + 5; Console.WriteLine(x * y); // Output: 50 Console.WriteLine(y % 3); // Output: 1
Operators should not be treated equally. They have a defined precedence which gives some operators priority over others. This is also known as the order of operations (PEMDAS).
It helps to know the order of operations when an expression contains more than one operator. Just like the order of operations in math, operators with higher precedence are calculated before the operators with a lower precedence. The parentheses operators () can be used to change the precedence. For example 2 + 4 will be calculated first in the expression 6 * (2 + 4).
Parentheses have the highest order of precedence. Arithmetic operators usually have the next highest order of precedence followed by binary, comparison, logical, and assignment operators.
The most helpful thing to remember is PEMDAS: Parentheses first, then Exponents, then Multiplication and Division, then Addition and Subtraction
When all operators in an expression have the same level of precedence, the order of operations is from left to right.
Example of using precedence
int x = 10; int y = 20; Console.WriteLine(x + y / 5); // Output: 14 Console.WriteLine((x + y) / 5); // Output: 6 Console.WriteLine(x + y - 5); // Output: 25
In the example above the first result you get from the expression x + y / 5 is 14. Using the mathematical order of operations, the value of y (20) is divided by 5 first since division has a higher priority. Then the value of x (10) is added, giving you a result of 14.
The second expression (x + y) / 5 gives you a result of 6. This is because the parentheses changed the precedence and gave x + y priority, causing it to calculate the addition before the division.
The last expression x + y - 5 returns a value of 25. The order of operations occurs from left to right since both operators have the same level of precedence.
This post touches the surface of working with operators in C#. I could definitely go on and write more but I think the post would become way too long. I plan on drilling into the categories of operators and sharing more examples in future posts.