In C#, operators are symbols used to perform operations on variables and values. They help manipulate data and perform calculations or comparisons.
Operators are a fundamental part of expressions and decision-making in C# programs.
Used to perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | int c = a + b; |
| - | Subtraction | int c = a - b; |
| * | Multiplication | int c = a * b; |
| / | Division | int c = a / b; |
| % | Modulus (remainder) | int c = a % b; |
Used to compare two values and return a boolean result.
| 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 |
Used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a > 0 && b > 0 |
| || | Logical OR | a > 0 || b > 0 |
| ! | Logical NOT | !isActive |
Used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Assign | a = 10; |
| += | Add and assign | a += 5; |
| -= | Subtract and assign | a -= 5; |
Operate on a single operand.
int x = 5;
x++;
--x;
Operators are essential for calculations, comparisons, and decision-making in C#. Mastering operators helps in writing efficient and readable code.