Skip to main content

Operators in C#

Operators in C# allow you to perform various operations on variables and values, such as arithmetic, comparison, logical, and more. This guide introduces the different types of operators available in C# and provides examples to help you understand how to use them.

1. Types of Operators in C#​

C# supports various operators, which can be broadly categorized as follows:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators
  • Ternary Operator

2. Arithmetic Operators​

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulus (Remainder)x % y

Example:​

int a = 10, b = 5;
Console.WriteLine(a + b); // Output: 15
Console.WriteLine(a - b); // Output: 5
Console.WriteLine(a * b); // Output: 50
Console.WriteLine(a / b); // Output: 2
Console.WriteLine(a % b); // Output: 0

3. Comparison Operators​

Comparison operators are used to compare two values.

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equalx >= y
<=Less than or equalx <= y

Example:​

int x = 10, y = 20;
Console.WriteLine(x == y); // Output: False
Console.WriteLine(x != y); // Output: True
Console.WriteLine(x > y); // Output: False
Console.WriteLine(x < y); // Output: True

4. Logical Operators​

Logical operators are used to combine multiple conditions.

OperatorDescriptionExample
&&Logical ANDx && y
``
!Logical NOT!x

Example:​

bool isAdult = true, hasID = false;
Console.WriteLine(isAdult && hasID); // Output: False
Console.WriteLine(isAdult || hasID); // Output: True
Console.WriteLine(!isAdult); // Output: False

5. Assignment Operators​

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assignx = 5
+=Add and assignx += 5
-=Subtract and assignx -= 5
*=Multiply and assignx *= 5
/=Divide and assignx /= 5
%=Modulus and assignx %= 5

Example:​

int num = 10;
num += 5; // num = 15
num -= 3; // num = 12
num *= 2; // num = 24
num /= 4; // num = 6
num %= 4; // num = 2

6. Unary Operators​

Unary operators work with a single operand.

OperatorDescriptionExample
+Unary plus+x
-Unary minus-x
++Incrementx++ or ++x
--Decrementx-- or --x
!Logical NOT!x

Example:​

int value = 5;
Console.WriteLine(value++); // Output: 5, value becomes 6
Console.WriteLine(++value); // Output: 7
Console.WriteLine(-value); // Output: -7

7. Ternary Operator​

The ternary operator (? :) is a shorthand for an if-else condition.

Syntax:​

condition ? trueValue : falseValue;

Example:​

int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(result); // Output: Adult

8. Conclusion​

Operators are fundamental to performing operations in C#. By using arithmetic, comparison, logical, and other operators effectively, you can manipulate and evaluate data in your programs with ease.