Operators to C#

Operators in C# are special symbols or keywords that allow you to perform different actions on variables and values in your program. Think of them as tools that help you manipulate data. They are essential in any programming language and are used for tasks like performing math calculations, comparing values, making decisions, and more.

Types of Operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
++ Increment a++ or ++a
-- Decrement a-- or --a
Example:
int a = 10;
int 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
a++;
Console.WriteLine(a); // Output: 11
b--;
Console.WriteLine(b); // Output: 4

2. Relational Operators

Relational operators are used to compare two values.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
Example:
int a = 10;
int b = 5;
Console.WriteLine(a == b); // Output: False
Console.WriteLine(a != b); // Output: True
Console.WriteLine(a > b);  // Output: True
Console.WriteLine(a < b);  // Output: False
Console.WriteLine(a >= b); // Output: True
Console.WriteLine(a <= b); // Output: False

3. Logical Operators

Logical operators are used to combine conditional statements.

Operator Description Example
&& Logical AND a && b
|| Logical OR a || b
! Logical NOT !a
Example:
bool a = true;
bool b = false;
Console.WriteLine(a && b); // Output: False
Console.WriteLine(a || b); // Output: True
Console.WriteLine(!a);     // Output: False

4. Bitwise Operators

Bitwise operators perform bit-by-bit operations.

Operator Description Example
& AND a & b
| OR a | b
^ XOR a ^ b
~ Complement ~a
<< Left shift a << 2
>> Right shift a >> 2
Example:
int a = 60;  // 0011 1100
int b = 13;  // 0000 1101
Console.WriteLine(a & b); // Output: 12  (0000 1100)
Console.WriteLine(a | b); // Output: 61  (0011 1101)
Console.WriteLine(a ^ b); // Output: 49  (0011 0001)
Console.WriteLine(~a);    // Output: -61 (1100 0011)
Console.WriteLine(a << 2);// Output: 240 (1111 0000)
Console.WriteLine(a >> 2);// Output: 15  (0000 1111)

5. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assign a = b
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
%= Modulus and assign a %= b
&&= Bitwise AND and assign a &= b
|= Bitwise OR and assign a |= b
^= Bitwise XOR and assign a ^= b
<<= Left shift and assign a <<= 2
>>= Right shift and assign a >>= 2
Example:
int a = 10;
int b = 5;
a += b; // a = a + b
Console.WriteLine(a); // Output: 15
a -= b; // a = a - b
Console.WriteLine(a); // Output: 10
a *= b; // a = a * b
Console.WriteLine(a); // Output: 50
a /= b; // a = a / b
Console.WriteLine(a); // Output: 10
a %= b; // a = a % b
Console.WriteLine(a); // Output: 0

6. Miscellaneous Operators

There are some additional operators in C# that are used in specific scenarios.

Operator Description Example
? : Ternary (conditional) a ? b : c
is Type compatibility obj is Type
as Type casting obj as Type
typeof Get type typeof(Type)
sizeof Get size of type sizeof(Type)
Example:
int a = 10;
int b = 5;
string result = (a > b) ? "a is greater" : "b is greater";
Console.WriteLine(result); // Output: a is greater
object obj = "Hello";
if (obj is string)
{
    Console.WriteLine("obj is a string"); // Output: obj is a string
}
string str = obj as string;
if (str != null)
{
    Console.WriteLine(str); // Output: Hello
}
Console.WriteLine(typeof(int)); // Output: System.Int32
Console.WriteLine(sizeof(int)); // Output: 4