Skip to content

Operators

Sann Lynn Htun edited this page Nov 19, 2024 · 1 revision

C# Operators

Operators in C# are symbols used to perform operations on variables and values. They include mathematical, logical, comparison, and assignment operators.

Types of Operators

1. Arithmetic Operators

Perform basic math operations.

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 5 / 2 2 (integer div.)
% Modulus (Remainder) 5 % 2 1

Example:

int a = 10, b = 3;
Console.WriteLine(a + b); // Output: 13
Console.WriteLine(a % b); // Output: 1

2. Relational (Comparison) Operators

Compare two values.

Operator Description Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 3 <= 5 true

Example:

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

3. Logical Operators

Combine multiple conditions.

Operator Description Example Result
&& Logical AND (5 > 3) && (8 > 6) true
` ` Logical OR
! Logical NOT !(5 > 3) false

Example:

bool isSunny = true, isWarm = false;
Console.WriteLine(isSunny && isWarm); // Output: False
Console.WriteLine(isSunny || isWarm); // Output: True

4. Assignment Operators

Assign values to variables.

Operator Description Example Equivalent To
= Assign x = 5 -
+= Add and assign x += 5 x = x + 5
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 2 x = x * 2
/= Divide and assign x /= 2 x = x / 2
%= Modulus and assign x %= 2 x = x % 2

Example:

int x = 10;
x += 5;  // x = 15
x *= 2;  // x = 30
Console.WriteLine(x); // Output: 30

5. Unary Operators

Operate on a single operand.

Operator Description Example Result
+ Unary plus +3 3
- Unary minus -3 -3
++ Increment x++ x + 1
-- Decrement x-- x - 1
! Logical NOT !true false

Example:

int y = 5;
Console.WriteLine(++y); // Output: 6 (Pre-increment)
Console.WriteLine(y--); // Output: 6 (Post-decrement)

6. Bitwise Operators

Perform operations on binary representations.

Operator Description Example Result
& Bitwise AND 5 & 3 1 (0101 & 0011)
` ` Bitwise OR `5
^ Bitwise XOR 5 ^ 3 6 (0101 ^ 0011)
~ Bitwise Complement ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2

Example:

int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
Console.WriteLine(a & b); // Output: 1
Console.WriteLine(a | b); // Output: 7

Summary

  • Arithmetic: Perform math (+, -, *, /, %).
  • Relational: Compare values (>, <, ==, !=).
  • Logical: Combine conditions (&&, ||, !).
  • Assignment: Assign values (=, +=, *=, etc.).
  • Unary: Single-operand operations (++, --, !).
  • Bitwise: Work with binary values (&, |, ^, ~).

C# operators enable efficient computation, condition checks, and value manipulation for various programming scenarios.

C# Basics Wiki

Core Concepts

Object-Oriented Programming (OOP)

Advanced Topics

Miscellaneous

Tools and Resources

Clone this wiki locally