Operators and expressions
↑Examples are independent C# snippets for modern .NET console projects unless noted otherwise. Class declarations and top-level code can live in Program.cs when top-level statements come first.
At a glance
Section titled “At a glance”Operators combine values into expressions. Their precedence matters, but parentheses are usually the clearest way to express intended order.
How it works
Section titled “How it works”Arithmetic and assignment
Section titled “Arithmetic and assignment”+, -, *, / and % perform arithmetic. When both operands of / are integers, the result uses integer division. +=, -=, *= and /= update the variable on the left.
int points = 11;points += 4;int groups = points / 4; // 3int remainder = points % 4; // 3Console.WriteLine($"{groups} groups, {remainder} left");Comparisons and logical operators
Section titled “Comparisons and logical operators”Comparison operators produce bool: ==, !=, <, <=, > and >=. Combine conditions with && (AND), || (OR) and ! (NOT). && and || short-circuit, meaning C# may not evaluate the right side.
int health = 30;bool hasShield = true;bool canContinue = health > 0 && (hasShield || health > 50);Console.WriteLine(canContinue); // TrueBuilt-in Math methods
Section titled “Built-in Math methods”Use Math.Min, Math.Max, Math.Abs, Math.Sqrt and Math.Round for common numeric tasks. For float-oriented Unity code you may also encounter Mathf, but that is a different API.
double distance = Math.Sqrt(3 * 3 + 4 * 4);int clamped = Math.Clamp(125, 0, 100);Console.WriteLine($"{distance}, {clamped}"); // 5, 100Deep dive: operator meaning depends on the operands
Section titled “Deep dive: operator meaning depends on the operands”Precedence and evaluation
Section titled “Precedence and evaluation”Multiplication binds more tightly than addition: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. Parentheses are useful even when you remember the precedence table because they communicate intent. An expression may also have side effects, such as count++; avoid squeezing several side effects into one hard-to-read line.
Division and remainders
Section titled “Division and remainders”When both operands are integers, / performs integer division: 7 / 2 is 3, not 3.5. Cast an operand or use floating-point literals when fractions matter. % produces the remainder, useful for alternating turns, partitioning IDs and wrapping an index around a known length. For negative inputs, C#’s remainder can be negative; do not blindly use it for array indexing.
Console.WriteLine(7 / 2); // 3Console.WriteLine(7 / 2.0); // 3.5Console.WriteLine(17 % 5); // 2Console.WriteLine((2 + 3) * 4); // 20&& and || short-circuit; & and | can do more
Section titled “&& and || short-circuit; & and | can do more”&& evaluates its right operand only when the left operand is true. || evaluates the right operand only when the left is false. This lets you safely write name != null && name.Length > 0. For boolean operands, & and | evaluate both sides; for integral operands they perform bitwise operations. Understand which operation you meant, especially when the right side changes data.
Assignment operators mutate state
Section titled “Assignment operators mutate state”score += 10 is a compound assignment, roughly meaning “add and store the result” with language-defined evaluation and conversion rules. count++ returns a value too; the prefix and postfix versions differ if you use that value. For routine updates, use one clear statement instead of clever chains that hide the order of changes.
Math APIs are tools, not magic
Section titled “Math APIs are tools, not magic”Math.Clamp(value, min, max) limits a value to a range at the call site, Math.Abs calculates absolute value, and Math.Round uses particular rounding rules (the default midpoint behavior may surprise you). Validate the expected numeric range and rounding mode for scoring, finance and gameplay. In Unity, Mathf offers common float-oriented counterparts.
When and why
Section titled “When and why”Use comparisons inside decisions and Math.Clamp when you need a value within an allowed range.
Common mistake
Section titled “Common mistake”= assigns while == compares; & and | are not always equivalent to short-circuiting && and ||.
Try it yourself
Section titled “Try it yourself”Write a condition checking that a level is between 1 and 10 inclusive.
Reveal a solution
level >= 1 && level <= 10.
