Curriculum
Operators in C# are special symbols used to perform operations on variables, values, and expressions. Operators in C# are essential for calculations, comparisons, decision-making, data manipulation, and business logic implementation. Every .NET developer uses Operators in C# extensively while developing Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, and Enterprise Software Solutions.
Understanding Operators in C# is important because almost every line of code written by a developer involves some form of operation on data.
Operators in C# are symbols that tell the compiler to perform specific operations on one or more operands.
Example:
int result = 10 + 20;
In this example:
Operators help developers perform mathematical calculations, comparisons, assignments, and logical evaluations.
Operators in C# help developers:
Without Operators in C#, software applications would not be able to process information effectively.
C# provides several categories of operators.
Arithmetic Operators are used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| – | Subtraction | 10 – 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
Example:
int sum = 10 + 20;
Console.WriteLine(sum);
Output:
30
Example:
int difference = 50 - 20;
Console.WriteLine(difference);
Output:
30
Example:
int product = 5 * 6;
Console.WriteLine(product);
Output:
30
Example:
int quotient = 60 / 2;
Console.WriteLine(quotient);
Output:
30
Example:
int remainder = 10 % 3;
Console.WriteLine(remainder);
Output:
1
The Modulus Operator returns the remainder after division.
Assignment Operators are used to assign values to variables.
Example:
int marks = 90;
Example:
marks += 10;
Equivalent to:
marks = marks + 10;
Example:
marks -= 5;
Example:
marks *= 2;
Example:
marks /= 2;
Assignment Operators make code shorter and easier to read.
Comparison Operators compare values and return a Boolean result.
| Operator | Description |
|---|---|
| == | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal To |
| <= | Less Than or Equal To |
Example:
Console.WriteLine(10 == 10);
Output:
True
Example:
Console.WriteLine(10 != 5);
Output:
True
Example:
Console.WriteLine(20 > 10);
Output:
True
Example:
Console.WriteLine(5 < 10);
Output:
True
Comparison Operators are commonly used in Conditional Statements and Loops.
Logical Operators combine multiple conditions.
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Both conditions must be true.
Example:
int age = 20;
int marks = 80;
Console.WriteLine(age >= 18 && marks >= 60);
Output:
True
At least one condition must be true.
Example:
Console.WriteLine(age >= 18 || marks >= 90);
Output:
True
Reverses the condition.
Example:
bool isBlocked = false;
Console.WriteLine(!isBlocked);
Output:
True
These operators increase or decrease values by one.
Example:
int count = 5;
count++;
Console.WriteLine(count);
Output:
6
Example:
int count = 5;
count--;
Console.WriteLine(count);
Output:
4
These operators are frequently used in loops.
Unary Operators work with a single operand.
Examples:
+number
-number
++count
--count
Unary Operators are useful for mathematical and logical operations.
The Ternary Operator provides a shorter alternative to if-else statements.
Syntax:
condition ? value1 : value2;
Example:
int marks = 75;
string result = marks >= 40 ? "Pass" : "Fail";
Console.WriteLine(result);
Output:
Pass
The Ternary Operator improves code readability for simple decisions.
The Null-Coalescing Operator (??) provides a default value when a variable is null.
Example:
string name = null;
string result = name ?? "Guest";
Console.WriteLine(result);
Output:
Guest
This operator is commonly used in ASP.NET Core applications.
Type Casting Operators convert values between data types.
Example:
double marks = 89.75;
int totalMarks = (int)marks;
Console.WriteLine(totalMarks);
Output:
89
The decimal portion is removed during conversion.
Operator Precedence determines the order of execution.
Example:
int result = 10 + 5 * 2;
Output:
20
Multiplication executes before addition.
Using parentheses:
int result = (10 + 5) * 2;
Output:
30
Parentheses have the highest precedence.
balance = balance + depositAmount;
percentage = totalMarks / totalSubjects;
finalPrice = productPrice - discount;
netSalary = basicSalary + bonus - deduction;
Operators in C# are essential for implementing business rules and calculations.
Operators in C# are heavily used in:
Strong knowledge of Operators in C# is essential for building professional .NET applications.
Operators in C# are symbols used to perform operations on variables and values.
Arithmetic Operators perform mathematical calculations such as addition, subtraction, multiplication, and division.
Comparison Operators compare values and return True or False.
Logical Operators combine multiple conditions for decision-making.
The Ternary Operator is a shorthand way to write simple if-else conditions.
Operators are essential for calculations, business logic, data processing, and decision-making in .NET applications.
WhatsApp us