C# - Operators
In C#, operators are special symbols used to perform operations on variables and values. They are categorized into several types based on their functionality. Here’s a comprehensive guide to the different types of operators in C#:
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Addition (
+
): Adds two operands.csharpint sum = 5 + 3; // 8
Subtraction (
-
): Subtracts the second operand from the first.csharpint difference = 5 - 3; // 2
Multiplication (
*
): Multiplies two operands.csharpint product = 5 * 3; // 15
Division (
/
): Divides the first operand by the second.csharpint quotient = 15 / 3; // 5
Modulus (
%
): Returns the remainder of division.csharpint remainder = 5 % 3; // 2
2. Relational Operators
Relational operators are used to compare two values and determine their relationship.
Equal to (
==
): Checks if two operands are equal.csharpbool isEqual = (5 == 3); // false
Not equal to (
!=
): Checks if two operands are not equal.csharpbool isNotEqual = (5 != 3); // true
Greater than (
>
): Checks if the first operand is greater than the second.csharpbool isGreater = (5 > 3); // true
Less than (
<
): Checks if the first operand is less than the second.csharpbool isLess = (5 < 3); // false
Greater than or equal to (
>=
): Checks if the first operand is greater than or equal to the second.csharpbool isGreaterOrEqual = (5 >= 3); // true
Less than or equal to (
<=
): Checks if the first operand is less than or equal to the second.csharpbool isLessOrEqual = (5 <= 3); // false
3. Logical Operators
Logical operators are used to perform logical operations on Boolean values.
Logical AND (
&&
): Returnstrue
if both operands are true.csharpbool result = (true && false); // false
Logical OR (
||
): Returnstrue
if at least one operand is true.csharpbool result = (true || false); // true
Logical NOT (
!
): Returns the inverse of the Boolean value.csharpbool result = !true; // false
4. Bitwise Operators
Bitwise operators perform operations on the binary representations of integers.
Bitwise AND (
&
): Performs a bitwise AND operation.csharpint result = 5 & 3; // 1 (binary 0101 & 0011 = 0001)
Bitwise OR (
|
): Performs a bitwise OR operation.csharpint result = 5 | 3; // 7 (binary 0101 | 0011 = 0111)
Bitwise XOR (
^
): Performs a bitwise XOR operation.csharpint result = 5 ^ 3; // 6 (binary 0101 ^ 0011 = 0110)
Bitwise NOT (
~
): Inverts all the bits of the operand.csharpint result = ~5; // -6 (binary ~0101 = 1010, two's complement)
Left Shift (
<<
): Shifts bits to the left, adding zeros on the right.csharpint result = 5 << 1; // 10 (binary 0101 << 1 = 1010)
Right Shift (
>>
): Shifts bits to the right, discarding the rightmost bits.csharpint result = 5 >> 1; // 2 (binary 0101 >> 1 = 0010)
5. Assignment Operators
Assignment operators are used to assign values to variables.
Assignment (
=
): Assigns the value on the right to the variable on the left.csharpint x = 5;
Add and assign (
+=
): Adds the right operand to the left operand and assigns the result to the left operand.csharpx += 3; // x = x + 3
Subtract and assign (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand.csharpx -= 2; // x = x - 2
Multiply and assign (
*=
): Multiplies the left operand by the right operand and assigns the result to the left operand.csharpx *= 4; // x = x * 4
Divide and assign (
/=
): Divides the left operand by the right operand and assigns the result to the left operand.csharpx /= 2; // x = x / 2
Modulus and assign (
%=
): Takes the modulus of the left operand by the right operand and assigns the result to the left operand.csharpx %= 3; // x = x % 3
6. Increment and Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by one.
Increment (
++
): Increases the value of the operand by one. Can be used as a prefix or postfix.csharpint a = 5; a++; // a = 6 (postfix) ++a; // a = 7 (prefix)
Decrement (
--
): Decreases the value of the operand by one. Can be used as a prefix or postfix.csharpint b = 5; b--; // b = 4 (postfix) --b; // b = 3 (prefix)
7. Conditional (Ternary) Operator
The conditional operator (? :
) is a shorthand for the if-else
statement.
Syntax:
csharpcondition ? value_if_true : value_if_false;
Example:
csharpint age = 20;
string result = (age >= 18) ? "Adult" : "Minor"; // "Adult"
8. Null-Coalescing Operators
Null-coalescing operators provide a way to handle null values.
Null-coalescing (
??
): Returns the left operand if it is notnull
; otherwise, it returns the right operand.csharpstring name = null; string displayName = name ?? "Default Name"; // "Default Name"
Null-coalescing assignment (
??=
): Assigns the right operand to the left operand only if the left operand isnull
.csharpstring name = null; name ??= "Default Name"; // name = "Default Name"
9. Member Access Operators
Dot Operator (
.
): Accesses members (properties, methods) of an object or class.csharpPerson person = new Person(); person.Name = "Alice"; // Accessing property person.Introduce(); // Calling method
Indexing Operator (
[]
): Accesses elements of an array or collection.csharpint[] numbers = { 1, 2, 3 }; int firstNumber = numbers[0]; // 1
Summary
Operators in C# are powerful tools for performing various operations on data. They include arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and null-coalescing operators. Understanding these operators and their proper usage is essential for writing effective and efficient C# code.