-
Notifications
You must be signed in to change notification settings - Fork 5
Operators
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Operators in C# are symbols used to perform operations on variables and values. They include mathematical, logical, comparison, and assignment 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 |
int a = 10, b = 3;
Console.WriteLine(a + b); // Output: 13
Console.WriteLine(a % b); // Output: 1
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 |
int x = 7, y = 10;
Console.WriteLine(x > y); // Output: False
Console.WriteLine(x <= y); // Output: True
Combine multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | (5 > 3) && (8 > 6) |
true |
` | ` | Logical OR | |
! |
Logical NOT | !(5 > 3) |
false |
bool isSunny = true, isWarm = false;
Console.WriteLine(isSunny && isWarm); // Output: False
Console.WriteLine(isSunny || isWarm); // Output: True
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 |
int x = 10;
x += 5; // x = 15
x *= 2; // x = 30
Console.WriteLine(x); // Output: 30
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 |
int y = 5;
Console.WriteLine(++y); // Output: 6 (Pre-increment)
Console.WriteLine(y--); // Output: 6 (Post-decrement)
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 |
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
Console.WriteLine(a & b); // Output: 1
Console.WriteLine(a | b); // Output: 7
-
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.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.