Skip to main content

Operators in C#

Ayesha
EditReport

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.

Video Explanationโ€‹

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.


Track Your Progress

Done with this topic? Mark it as complete to track your progress.

๐Ÿ’ฌ Discuss this page

Have a question or spot something confusing in "Operators in C#"? Ask below โ€” it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.