Operators and Expressions in .NET

Operators are special symbols that perform operations on one or more operands. An expression is a combination of operands, operators, and values that evaluates to a single value.

Types of Operators

The .NET platform supports a wide range of operators, categorized by their functionality:

Arithmetic Operators

Used for mathematical calculations.

Operator Description Example
+ Addition int sum = 5 + 3; // sum is 8
- Subtraction int difference = 10 - 4; // difference is 6
* Multiplication int product = 6 * 7; // product is 42
/ Division int quotient = 15 / 3; // quotient is 5
% Modulo (Remainder) int remainder = 10 % 3; // remainder is 1
++ Increment int count = 5; count++; // count is 6
-- Decrement int value = 10; value--; // value is 9

Comparison Operators

Used to compare two values. They return a boolean value (true or false).

Operator Description Example
== Equal to bool isEqual = (5 == 5); // isEqual is true
!= Not equal to bool isNotEqual = (5 != 3); // isNotEqual is true
> Greater than bool isGreater = (10 > 5); // isGreater is true
< Less than bool isLess = (3 < 7); // isLess is true
>= Greater than or equal to bool isGreaterOrEqual = (8 >= 8); // isGreaterOrEqual is true
<= Less than or equal to bool isLessOrEqual = (2 <= 5); // isLessOrEqual is true

Logical Operators

Used to combine or modify boolean expressions.

Operator Description Example
&& Logical AND (Short-circuiting) bool result = (true && false); // result is false
|| Logical OR (Short-circuiting) bool result = (true || false); // result is true
! Logical NOT bool result = !true; // result is false
^ Logical XOR bool result = (true ^ false); // result is true
& Bitwise AND (also used for non-short-circuiting logical AND) bool result = (true & false); // result is false
| Bitwise OR (also used for non-short-circuiting logical OR) bool result = (true | false); // result is true

Assignment Operators

Used to assign values to variables.

Operator Description Example
= Assignment int x = 10;
+= Add and assign x += 5; // x is now 15
-= Subtract and assign x -= 3; // x is now 12
*= Multiply and assign x *= 2; // x is now 24
/= Divide and assign x /= 4; // x is now 6
%= Modulo and assign x %= 5; // x is now 1

Other Operators

Includes unary, ternary, and type-testing operators.

Operator Description Example
?: Ternary conditional operator string message = (age >= 18) ? "Adult" : "Minor";
is Type testing if (obj is string) { ... }
as Type conversion string str = obj as string;
new Object creation MyClass instance = new MyClass();
. Member access Console.WriteLine(myObject.Name);
[] Array indexing/indexer access int element = myArray[0];

Operator Precedence and Associativity

Operators have a defined order of execution, known as precedence. When operators have the same precedence, associativity rules determine the order of evaluation (left-to-right or right-to-left).

Example: Operator Precedence

In the expression 2 + 3 * 4, multiplication (*) has higher precedence than addition (+). Therefore, 3 is multiplied by 4 first, and then 2 is added to the result.


int result = 2 + 3 * 4; // Evaluates to 14 (3 * 4 = 12, then 2 + 12 = 14)
int anotherResult = (2 + 3) * 4; // Evaluates to 20 (parentheses override precedence)
                
Note: Understanding operator precedence and associativity is crucial for writing correct and predictable code. Parentheses can be used to explicitly control the order of operations.

Expressions

An expression is a sequence of operators and operands that forms a statement that can be evaluated. Expressions can be simple, like a literal value or a variable, or complex, involving multiple operators and function calls.

Expression Examples:

Expression Evaluation

When an expression is evaluated, the result is a single value. The type of the result depends on the types of the operands and the operations performed.

Example: Complex Expression


// Assuming Person class has Name (string) and Age (int) properties
Person person = new Person { Name = "Bob", Age = 30 };
bool isAdultAndNameBob = (person.Age >= 18) && (person.Name == "Bob");
// isAdultAndNameBob evaluates to true