C# Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within C# that will be explained below.

Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within a C# script.

Operator Description
+ Adds two values together.
Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.
% Calculates the remainder after one value is divided by another.
++ Increments a value by 1.
Decrements a value by 1.

The following examples show each of these operators in action.

// Variables to use in expressions.
int a = 10, b = 20, c = 0;

c = a + b;   // c will equal 30.
c = b - a;   // c will equal 10.
c = a * b;   // c will equal 200.
c = b / a;   // c will equal 2.
c = b % a;   // c will equal 0.
c = a++;     // c will equal 11.
c = b--;     // c will equal 19.

Assignment Operators

Assignment operators in C# are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator Description
= Assigns the value of the right side operand to the left side operand.
+= Adds the value from the right side operand to the left side operand and assigns the value to the left side operand.
-= Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand.
*= Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand.
/= Divides the left side operand by the value of the right side operand and assigns the value to the left side operand.
%= Takes the resulting modulus of the operands on both sides and assigns it to the left hand operand.

The examples below show these assignment operators in action.

int a = 10; // a gets set to 10.

int b = 2;
b += 5; // b gets set to 7.

int c = 10;
c -= 4; // c gets set to 6.

int d = 2;
d *= 2; // d gets set to 4.

int e = 20;
e /= 5; // e gets set to 4.

int f = 5;
f %= 2; // f gets set to 1.

Relational Operators

Relational operators compare two values and are used, for example, in ‘if’ statements, which are described in the section on Decision Making. The result of the comparison is either True or False.

Operator Description
== Evaluates to True if the two values are equal.
!= Evaluates to True if the two values are not equal.
> Evaluates to True if the first value is greater than the second.
< Evaluates to True if the first value is less than the second.
>= Evaluates to True if the first value is greater than or equal to the second.
<= Evaluates to True if the first value is less than or equal to the second.

Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an ‘if’ statement, which is discussed in the section on Decision Making.

Operator Description
&& Logical AND operator where operands on both sides must be True to evaluate to True.
|| Logical OR operator where only one of the two operands must be True to evaluate to True.
! Logical NOT operator that can be used in conjunction with ‘&&’ or ‘||’ to reverse the resulting operand. In the case of ‘&&’, if both conditions evaluate to True, then with the use of the ‘!’ operator the result would be False.