C# (C Sharp) is a programming language that provides various operators to perform different operations on variables and values. Here are the commonly used operators in C#:
- Arithmetic Operators:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second.
- Modulus (%): Returns the remainder after division.
- Assignment Operators:
- Assignment (=): Assigns a value to a variable.
- Addition assignment (+=): Adds the value of the right operand to the left operand and assigns the result to the left operand.
- Subtraction assignment (-=): Subtracts the value of the right operand from the left operand and assigns the result to the left operand.
- Multiplication assignment (*=): Multiplies the left operand by the value of the right operand and assigns the result to the left operand.
- Division assignment (/=): Divides the left operand by the value of the right operand and assigns the result to the left operand.
- Modulus assignment (%=): Computes the modulus of the left operand with the value of the right operand and assigns the result to the left operand.
- Comparison Operators:
- 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.
- Logical Operators:
- Logical AND (&&): Returns true if both operands are true.
- Logical OR (||): Returns true if at least one of the operands is true.
- Logical NOT (!): Reverses the logical state of the operand.
- Bitwise Operators:
- Bitwise AND (&): Performs a bitwise AND operation between two operands.
- Bitwise OR (|): Performs a bitwise OR operation between two operands.
- Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between two operands.
- Bitwise NOT (~): Performs a bitwise NOT (one’s complement) operation on a single operand.
- Left shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
- Right shift (>>): Shifts the bits of the left operand to the right by a specified number of positions.
These are some of the fundamental operators in C#. Additionally, there are also conditional operators (ternary operator) and other specialized operators like the null coalescing operator (??) and the is/as operators for type checking and casting, among others.
Precedence of Operators in C# :
In C#, operators have different levels of precedence, which determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. Here’s a list of C# operators arranged in order of their precedence, from highest to lowest:
- Postfix operators:
- () Method call
- [] Array indexing
- . Member access
- ++ Post-increment
- — Post-decrement
- Unary operators:
-
- Unary plus
-
- Unary minus
- ! Logical NOT
- ~ Bitwise NOT
- ++ Pre-increment
- — Pre-decrement
- (type) Cast
-
- Multiplicative operators:
-
- Multiplication
- / Division
- % Modulus
-
- Additive operators:
-
- Addition
-
- Subtraction
-
- Shift operators:
- << Left shift
-
Right shift
- Relational and type testing operators:
- < Less than
-
Greater than
- <= Less than or equal to
-
= Greater than or equal to
- is Type testing operator
- as Type casting operator
- Equality operators:
- == Equal to
- != Not equal to
- Bitwise AND operator:
- & Bitwise AND
- Bitwise XOR operator:
- ^ Bitwise XOR
- Bitwise OR operator:
- | Bitwise OR
- Conditional AND operator:
- && Conditional AND
- Conditional OR operator:
- || Conditional OR
- Null-coalescing operator:
- ?? Null-coalescing
- Conditional operator:
- ?: Ternary conditional
- Assignment and lambda operators:
- = Assignment
- += Addition assignment
- -= Subtraction assignment
- *= Multiplication assignment
- /= Division assignment
- %= Modulus assignment
- &= Bitwise AND assignment
- |= Bitwise OR assignment
- ^= Bitwise XOR assignment
- <<= Left shift assignment
-
= Right shift assignment
- => Lambda operator
It’s important to note that parentheses can be used to override the default precedence and explicitly define the order of evaluation in an expression.