Operators in .NET Core

Operators are special symbols that perform operations on one or more values (operands).

Arithmetic Operators

These operators are used for mathematical calculations.

Operator Name Description
+ Addition Adds two operands. Can also be used for string concatenation.
- Subtraction Subtracts the right operand from the left operand.
* Multiplication Multiplies two operands.
/ Division Divides the left operand by the right operand.
% Modulo Returns the remainder of the division.
++ Increment Increases the value of an operand by one.
-- Decrement Decreases the value of an operand by one.

Example:


int a = 10;
int b = 5;
int sum = a + b;       // sum is 15
int difference = a - b; // difference is 5
int product = a * b;   // product is 50
int quotient = a / b;  // quotient is 2
int remainder = a % b; // remainder is 0

Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {difference}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
Console.WriteLine($"Remainder: {remainder}");

a++; // a becomes 11
b--; // b becomes 4
Console.WriteLine($"Incremented a: {a}");
Console.WriteLine($"Decremented b: {b}");
            

Comparison Operators

These operators are used to compare two values.

Operator Name Description
== Equal to Checks if two operands are equal.
!= Not equal to Checks if two operands are not equal.
> Greater than Checks if the left operand is greater than the right operand.
< Less than Checks if the left operand is less than the right operand.
>= Greater than or equal to Checks if the left operand is greater than or equal to the right operand.
<= Less than or equal to Checks if the left operand is less than or equal to the right operand.

Example:


int x = 7;
int y = 10;

bool isEqual = (x == y);      // false
bool isNotEqual = (x != y);   // true
bool isGreater = (x > y);     // false
bool isLess = (x < y);        // true

Console.WriteLine($"x == y: {isEqual}");
Console.WriteLine($"x != y: {isNotEqual}");
Console.WriteLine($"x > y: {isGreater}");
Console.WriteLine($"x < y: {isLess}");
            

Logical Operators

These operators are used to combine conditional statements.

Operator Name Description
&& Logical AND Returns true if both statements are true.
|| Logical OR Returns true if one of the statements is true.
! Logical NOT Reverses the result.

Example:


int age = 25;
bool hasLicense = true;

if (age >= 18 && hasLicense) {
    Console.WriteLine("You are eligible to drive.");
}

bool isSunny = false;
if (!isSunny || age < 30) {
    Console.WriteLine("It's either not sunny, or you are younger than 30.");
}
            
Tip:

Understand operator precedence to ensure your expressions are evaluated as you intend. For example, multiplication is performed before addition.

Assignment Operators

These operators are used to assign values to variables.

Operator Example Same As
= c = a c = a
+= c += a c = c + a
-= c -= a c = c - a
*= c *= a c = c * a
/= c /= a c = c / a
%= c %= a c = c % a

Other Operators

There are many other operators, including bitwise operators, null-coalescing operators, and conditional (ternary) operators.

Example:


int time = 20;
string timeOfDay = (time < 18) ? "Good day" : "Good evening";
Console.WriteLine(timeOfDay); // Output: Good evening

string name = null;
string displayName = name ?? "Guest";
Console.WriteLine(displayName); // Output: Guest