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.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Let’s dive in.

Type of Operators in C#

In C# there are many different operators to choose from, but let’s focus on the most important ones.

The most used operators in C# are:

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, <, <=, >, >=
  • Logical: !, &&, ||
  • Increment/Decrement ++, – –

Let’s see how they work.

Arithmetic Operators in C#

Arithmetic operators are defined for all numeric data types and include +, -, *, and / for basic binary arithmetic operations (addition, subtraction, multiplication, and division), and % for the remainder after division.

It’s important to note that the + operator has different behavior for numbers and strings. When used with numbers, the expression 5 + 5 evaluates to 10, but when used with strings, the expression “5” + “5” evaluates to “55” which is string concatenation, not addition.

Relational Operators in C#

All relational operators return either a true or false result. They are used to compare expressions or variables on both sides of the operator. These operators have lower priority than arithmetic ones. In the example: x*a-8*b > y+5*z;, the calculation on the left side of the greater than operator is performed first, followed by the calculation on the right side, and then they are compared.

For value-type variables and strings, the equality (==) operator returns true only if they are identical, otherwise, it returns false. However, if the variables are reference types, the == operator returns true only if they reference the same memory location, otherwise, it returns 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";

        var student1 = new Student("John", 25);
        var student2 = new Student("John", 25);

        Console.WriteLine(a == b); // true
        Console.WriteLine(s1 == s2); // true
        Console.WriteLine(student1 == student2); // false
    }
}

We can see that both variables a and b are equal, as are s1 and s2.

However, student1 and student2 are not equal as they refer to different memory locations. If we create another variable of the type Student and assign the value of student1 to it, the == operator will return true:

var student1 = new Student("John", 25);
var student2 = new Student("John", 25);

var student3 = student1;

Console.WriteLine(student1 == student3); // true

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--;

        Console.WriteLine(a); // 14
        Console.WriteLine(b); // 14
        Console.WriteLine(c); // 19
        Console.WriteLine(d); // 20
    }
}

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++;

        Console.WriteLine(a); // 16
        Console.WriteLine(b); // 16
        Console.WriteLine(c); // 21
        Console.WriteLine(d); // 20
    }
}

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#.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!