What are C# operators and how to use them
Get acquainted with C# operators and the actions they can perform. We will find out what are C# operators. What categories of operators do we find in C#? In the end, we will look over expressions and how should we work with them.
Before going into details about C# operators, let’s relax with a joke:
The programmer’s spouse asks the programmer to go to the store
Spouse says, “Get a loaf of bread, if they have eggs, get a dozen.”
The programmer comes home with a dozen loaves of bread and says, “They had eggs.”
What are C# operators?
Every programming language uses operators. An operator in C# is a symbol that tells the compiler to perform specific operations and produce the final result. Operators allow the processing of primitive data types and objects.
C# operator categories
C# operators separate into a few different categories:
- Arithmetic operators – perform mathematical operations..
- Assignment operators – allow assigning values to variables. The operator used to achieve this is “=”.
- Comparison operators – allow comparison of two variables.
- Logical operators – these operators work with Boolean data types and Boolean expressions.
- Binary operators – performing operations on the binary representation of numerical data.
- Type conversion operators – allow conversion of data from one type to another.
- Let’s go into some details about the most used C# operators.
C# Arithmetical Operators
C# arithmetical operators (+, -, *, /) are the same as the ones in math. They perform addition, subtraction, multiplication, and division on numerical values. The result of an arithmetical operator is always a numerical value. Besides these operators, we also have operators for increasing by one (++) and decreasing by one (–). One thing to have in mind when using division is, division by 0 is not allowed. If you want to get the rest of the integer division of integers you can use the operator %.
Some examples of arithmetical operators:
var a = 3;
var b = 2;
Console.WriteLine(a + b); // 5
Console.WriteLine(a - b); // 1
Console.WriteLine(a * b); // 6
Console.WriteLine(9 / a); // 3
Console.WriteLine(a + (b++)); // 5
Console.WriteLine(a + (++b)); // 7
Console.WriteLine(10 % a); // 1
Console.WriteLine(a / 0); // DivideByZeroException
C# Comparison operators
When you want to compare two or more variables in C#, look no further than the comparison operators. C# supports the following comparison operators:
- greater than (>)
- less than (<)
- greater than or equal to (>=)
- less than or equal to (<=)
- equality (==)
- difference (!=)
Here are some examples of C# comparison operators:
var a = 3;
var b = 2;
Console.WriteLine(a > b); // true
Console.WriteLine(a < b); // false
Console.WriteLine(a >= b); // true
Console.WriteLine(a <= b); // false
Console.WriteLine(a == b); // false
Console.WriteLine(a != b); // true
C# Logical Operators
The C# logical operators take Boolean values and return a Boolean result (true or false). These operators are “AND” (&&), “OR” (||), “exclusive OR” (^), and logical negation (!).
“AND” (&&) returns true only when both variables are true. The logical operator “OR” (||) returns true when one of the variables is true. C# logical negation operator (!) changes the value of the variable. If the variable is true, after applying the negation operator to our variable, the result will be false.
Here are some examples of C# logical operators:
var a = true;
var b = false;
Console.WriteLine(a && b); // false
Console.WriteLine(a || b); // true
Console.WriteLine(!a); // false
Console.WriteLine(a && true); // true
Console.WriteLine(b && true); // false
C# Type conversion operators
The type conversion operator helps to convert a variable from one type to another. These are the operators that fall under this category:
- “as” – this is used for type conversion. Invalid conversion returns null, not an exception. Remember that “as” can be used only on reference type variables.
- “new” – this is used to create and initialize new objects.
- “is” – this operator is used to check if an object is compatible with a specific type.
- “??” – this is similar to a conditional operator. Checks a variable for null and returns the specified value from the right of the operator.
string emptyString = null;
Console.WriteLine(emptyString ?? "string is empty"); // string is empty
var validString = "valid string";
Console.WriteLine(validString ?? "string is empty"); // valid string
Here are a few examples of how you can convert objects from one type to another. In this example, we will try multiple ways of converting and casting from one type to another.
var stringValue = "10";
Console.WriteLine(Convert.ToInt32(stringValue)); // 10
Console.WriteLine(Int32.Parse(stringValue)); // 10
double doubleValue = 10.5;
Console.WriteLine((float)doubleValue); // 10.5
Console.WriteLine(Convert.ToString(doubleValue)); // 10.5
object genericObject = "10";
Console.WriteLine(genericObject as string); // 10
My main recommendation when working with operators in C# is to use brackets whenever there is the slightest doubt about the priorities of the operations. Besides this, brackets will make the code way easier to read.
This is it for now. If you have any questions or have some suggestions to help me improve this post, please use the comments section below.