In C#, operators and expressions are core building blocks of logic and computation. Operators are symbols that perform operations on variables and values, while expressions are combinations of variables, values, operators, and method calls that produce a result. This guide provides a complete reference on C# operators with examples, descriptions, and best practices.
An operator in C# is a special symbol or keyword used to perform operations like addition, subtraction, comparison, logical evaluation, and more. Operators act on operands to form expressions.
C# provides a wide range of operators categorized as follows:
Arithmetic operators are used for basic mathematical operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
int a = 10;
int b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 1
Used to compare two values. The result is a boolean (true or false).
| 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 |
int x = 7, y = 5;
Console.WriteLine(x == y); // False
Console.WriteLine(x != y); // True
Console.WriteLine(x > y); // True
Console.WriteLine(x < y); // False
Console.WriteLine(x >= y); // True
Console.WriteLine(x <= y); // False
Used to combine multiple boolean expressions.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a && b |
| || | Logical OR | a || b |
| ! | Logical NOT | !a |
bool a = true, b = false;
Console.WriteLine(a && b); // False
Console.WriteLine(a || b); // True
Console.WriteLine(!a); // False
Used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Assign | a = 10 |
| += | Add and assign | a += 5 |
| -= | Subtract and assign | a -= 2 |
| *= | Multiply and assign | a *= 3 |
| /= | Divide and assign | a /= 4 |
| %= | Modulus and assign | a %= 2 |
int a = 10;
a += 5; // a = 15
a *= 2; // a = 30
a /= 3; // a = 10
Operators that act on a single operand.
int x = 5;
Console.WriteLine(+x); // 5
Console.WriteLine(-x); // -5
Console.WriteLine(++x); // 6
Console.WriteLine(--x); // 5
Console.WriteLine(x++); // 5 (then x becomes 6)
Console.WriteLine(x--); // 6 (then x becomes 5)
Used to manipulate individual bits.
| Operator | Description | Example |
|---|---|---|
| & | AND | a & b |
| | | OR | a | b |
| ^ | XOR | a ^ b |
| ~ | Complement | ~a |
| << | Left shift | a << 1 |
| >> | Right shift | a >> 1 |
int a = 5; // 0101
int b = 3; // 0011
Console.WriteLine(a & b); // 0001 -> 1
Console.WriteLine(a | b); // 0111 -> 7
Console.WriteLine(a ^ b); // 0110 -> 6
Console.WriteLine(~a); // Complement
Console.WriteLine(a << 1); // 1010 -> 10
Console.WriteLine(a >> 1); // 0010 -> 2
Shorthand for if-else.
int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(result); // Adult
Returns the left operand if itβs not null, otherwise returns the right operand.
string name = null;
string finalName = name ?? "Default Name";
Console.WriteLine(finalName); // Default Name
Assigns value to a variable only if it is null.
string message = null;
message ??= "Hello!";
Console.WriteLine(message); // Hello!
object obj = "Hello";
if (obj is string)
{
Console.WriteLine("It is a string.");
}
object data = "World";
string text = data as string;
Console.WriteLine(text); // World
C# evaluates operators in a specific order (precedence). Parentheses can change this behavior.
int result = 10 + 5 * 2; // result = 20
int changed = (10 + 5) * 2; // changed = 30
An expression is a combination of literals, variables, operators, and method calls that result in a value.
int a = 5, b = 10;
int sum = a + b; // Arithmetic expression
bool isGreater = a < b; // Boolean expression
string fullName = "John" + " " + "Doe"; // String expressionC# offers a rich set of operators that allow you to perform various operations efficiently. From arithmetic and comparison to bitwise and logical evaluations, mastering operators and expressions is essential for any C# programmer. With clear understanding and proper usage, you can write clean, readable, and performant code. As you work on real-world applications, combining multiple operators in expressions becomes a daily necessity. Keep practicing with different examples to reinforce your learning of this foundational topic in C# development.
Copyrights © 2024 letsupdateskills All rights reserved