C# operators

Symbols like *, /, +, == etc. in C# programming are called operators and allow developers to do certain operations.

For example, the / symbol is called division operator and is used to divide two values to each other.

Or the - symbol is called subtraction operator and is used to subtract two values from each other.

In short there are 5 categories of operators and in the next couple of sections we will dive deep in each of these categories:

Name: Example:
Arithmetic Operators +, -, * etc.
Assignment Operators =, -=, += etc.
Comparison Operators <, >, <=, >=, == etc.
Logical Operators &&, ||, !
Bit fiddling operators (AKA bitwise operators) <<, >>, ^, &, |

Before jumping into those sections, there are a couple of concepts that we need to learn:

Operators like *, /, + etc. involve two values.

For example:

`3+2`;

The value 3 on the left side of the + operator is called left operand and the value 2 on the right side of the operator is called right operand. An operand can be either a variable (that has a value already assigned to it), a constant value (like 1, 2, 3 etc.) or a method that returns a value.

Together operand and operators create expression.

Note: the keynote about expression is that it should return a value.

For example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
int result = 2 * 4;
Console . WriteLine(result);
}
}
}

Output: 8

In this example we’ve created the variable named result which is of type int and for its value we have the 2 * 4 expression.

The operands in this expression are 2 and 4. Also the operator of this expression is multiplication *.

As mentioned before, expressions always return a result. So first this expression will be resolved and then the result will be assigned as the value of the result variable.

LValues and RValues:

When we work with assignment operators like =, the operand on the right side of the operator is called Rvalue and the operand on the left side of the operator is called LValue.

You should know that we can’t put constant values (like 1,2,3,4 etc.) or a function call on the left side of assignment operators. Basically such values can’t be LValues

The LValues are considered as the receivers or containers. Basically, it's a place where we can store values. So because variables point to memory space and values can be stored in this space, they are perfect candidate for being LValues.

For example:

int jackSalary = 200000;
5000 = jackSalary;

The first assignment int jackSalary = 200000; is acceptable because the LValue is a variable that points to a memory space.

But the second assignment causes the compiler to return error. This is because the value 5000 is a constant value and does not point to any memory location that allows for storing a value.

On the other hand, the RValues can be a constant value (like c, f, 10, 233.322 etc.), a variable, an expression that results a value or a method call that returns a value.

Example:

int jackSalary = 200000;
int ellenSalary = 160000;
int totalSalary = jackSalary + ellenSalary;

In the third assignment of the example above, because both jackSalary and ellenSalary are pointing to values, they can be used as the RValues in this expression. So the final value that will be stored in the totalSalary variable is: 36000