C# Operators

In this lesson, you will learn about C# Operators, Operators categories, and their usage, along with examples to better understand the topic.


What are C# Operators?

In C#, operators are used to performing operations and manipulate variables.

There are four main categories of operators in C#

  • Operators Arithmetic
  • Operators Assignment
  • Operators Comparison
  • Operators Logical

Arithmetic Operators

In C#, Arithmetic operators perform operations, such as addition or multiplication, on variables.

For Example, the below is an addition (+) operation in C#:

Arithmetic Operators are used to perform operations on both variables and values. For Example:

List of Arithmetic Operators

Arithmetic Operators SymbolArithmetic Operators’ NamesExample
+Additiona + b
-Substractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponentiationa ** b
++Incrementa++
--Decrementa--

Example of Arithmetic Operators in C#

Below is an example that covers all Arithmetic Operations in C#:

Assignment Operators

Assignment operators are another type of operator in C#, which is used to assign a value to a variable. For Example, the code below assigns a value of 5 to variable a.

int a = 5;

In addition, the Assignment operator += add a value to a variable.

For Example:

List of Assignment Operators in C#

Assignment Operators SymbolExplanationExample
=a = b + c will allocate the value of b + c into
a
a = b + c
+=a += b is same to a = b + aa+=b
-=a -= b is same to a = a - b a-=b
*=a *= b is same to a = a * ba*=b
/=a /= b is same to a = a / ba/=b
%=a %= b is same to a = a % ba%=b
&=a &= b is same to a = a & ba&=b
|=a |= b is same to a = a | ba|=b
^=a ^= b is same to a = a ^ ba^=b
<<=a <<= b is same to a = a << ba <<= b
>>=a >>= b is same to a = a >> ba >>= b

Example of Assignment Operators in C#

C# Comparison Operators

Comparison operators are used to comparing between two values. If the condition is met, then the result is true. Otherwise, the result is false.

For example, the > check if the value a is greater than value b:

List of Comparison Operators in C#

Arithmetic Operators SymbolArithmetic Operators NameExample
=Equala = b
! =Not equala != b
>Greater thana > b
>=Greater than equal toa >= b
<Less thana < b
<=Less than equal toa <= b

Example of Comparison Operators in C#

C# Logical Operators

The last group of C# Operators is Logical Operators. They are used to determine the logic between two variables or values. For example, the code below explains the “and” operator &&. It checks if “a” is less than 10 and “a” is greater than 4.

int a = 5;

Console.WriteLine(a < 10 && a >4 ); // Returns True

List of Logical Operators in C#

Logical Operators SymbolOperators NameExample
&&ANDa && b
||ORa || b
!Not!a

Example of Logical Operators in C#

This concludes the C# Operators Lesson. In the next lesson, you will learn about the string type in C#, string concatenation, and other topics related to the string reference type.