Elementary Overview
An operator tells C# what operation to perform on one or more values. The values are operands, and the combination of operators and operands forms an expression. C# uses operators for arithmetic, comparison, assignment, Boolean logic, incrementing values, null handling, and many other tasks.
This lesson continues directly from OSC#.002: C# Variables and Built-In Types. It also builds on the toolchain established in OSC#.001: .NET SDK and Compiler and the runtime foundation introduced in OSDotNet.001: What Is .NET?.
What You Should Learn
- How arithmetic operators work with numeric types.
- How assignment and compound-assignment operators update variables.
- How comparison operators produce Boolean results.
- How
&&,||, and!implement Boolean logic. - Why
&&and||short-circuit. - How prefix and postfix
++and--differ inside larger expressions. - How the conditional operator
?:selects one of two values. - How
??and??=help with nullable values. - How operator precedence and parentheses determine evaluation order.
Arithmetic Operators
The basic arithmetic operators are +, -, *, /, and %. The remainder operator % returns the remainder after division and is useful for tasks such as detecting even and odd numbers or cycling through fixed ranges.
int a = 17;
int b = 5;
Console.WriteLine(a + b); // 22
Console.WriteLine(a - b); // 12
Console.WriteLine(a * b); // 85
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 2
Because both operands in a / b are integers, integer division discards the fractional part. To preserve a fraction, at least one operand must use a floating-point or decimal type.
double ratio = 17.0 / 5;
Console.WriteLine(ratio); // 3.4
Assignment and Compound Assignment
The simple assignment operator = stores the value produced by the right-hand expression in the left-hand variable. Compound assignment operators combine an operation with assignment. For example, x += 5 means “add five to x and store the result back in x.”
int miners = 100;
miners += 20; // 120
miners -= 5; // 115
miners *= 2; // 230
miners /= 10; // 23
miners %= 6; // 5
Console.WriteLine(miners);
C# also provides compound operators for bitwise and shift operations such as &=, |=, ^=, <<=, and >>=. Those become more important in later lessons on systems, flags, binary data, and hardware-oriented programming.
Comparison Operators
Comparison operators evaluate relationships between values and produce true or false. The common operators are ==, !=, <, >, <=, and >=.
int temperature = 78;
bool hot = temperature > 75;
bool exact = temperature == 78;
bool safe = temperature <= 85;
bool different = temperature != 70;
Console.WriteLine(hot);
Console.WriteLine(exact);
Console.WriteLine(safe);
Console.WriteLine(different);
Do not confuse = with ==. The first assigns a value; the second asks whether two operands are equal. C# catches many accidental assignment mistakes because an assignment expression must still produce a type that is valid in the surrounding context.
Boolean Logical Operators
For Boolean values, && means logical AND, || means logical OR, and ! means logical NOT. These operators let programs combine smaller conditions into larger expressions.
bool powerAvailable = true;
bool coolingHealthy = true;
bool maintenanceMode = false;
bool canRun = powerAvailable && coolingHealthy && !maintenanceMode;
Console.WriteLine(canRun);
&& and || are conditional logical operators. They short-circuit: if the left side of && is already false, the right side is not evaluated; if the left side of || is already true, the right side is not evaluated. That behavior can improve efficiency and can also prevent invalid operations such as dereferencing a value that was first checked for null.
string? status = null;
if (status != null && status.Length > 0)
{
Console.WriteLine(status);
}
Increment and Decrement
The increment operator ++ adds one, while -- subtracts one. When used by themselves as statements, prefix and postfix forms appear to do the same thing. Inside a larger expression, however, the difference matters.
int x = 5;
int a = x++; // a gets 5, then x becomes 6
int y = 5;
int b = ++y; // y becomes 6, then b gets 6
Console.WriteLine($"x={x}, a={a}");
Console.WriteLine($"y={y}, b={b}");
Postfix returns the old value before the increment or decrement takes effect in the surrounding expression. Prefix updates the variable first and then produces the new value. Avoid compressing too many side effects into one expression; readable code is usually easier to verify.
The Conditional Operator
The conditional operator ?: is C#’s ternary operator. It evaluates a Boolean condition and chooses one of two expressions.
double temperature = 84.5;
string state = temperature > 80 ? "hot" : "normal";
Console.WriteLine(state);
Use the conditional operator when the choice is simple and value-oriented. If the logic requires several statements, nested decisions, or substantial side effects, an if/else statement will usually be clearer.
Null-Coalescing Operators
The null-coalescing operator ?? supplies a fallback when the left operand is null. The null-coalescing assignment operator ??= assigns a value only when the left side is currently null.
string? rackName = null;
string displayName = rackName ?? "Unassigned Rack";
rackName ??= "Rack A";
Console.WriteLine(displayName);
Console.WriteLine(rackName);
These operators are especially useful in modern C# because nullable reference types make null handling explicit. Later lessons will cover nullable types, null-conditional access with ?., and more advanced pattern matching in greater depth.
Operator Precedence and Associativity
When several operators appear in one expression, precedence determines which operators bind more tightly. Multiplication and division bind more tightly than addition and subtraction, so 3 + 4 * 2 evaluates to 11. Parentheses override the normal precedence and communicate intent directly to the reader.
int first = 3 + 4 * 2; // 11
int second = (3 + 4) * 2; // 14
Console.WriteLine(first);
Console.WriteLine(second);
Associativity resolves operators at the same precedence level. Many binary operators associate left to right, while assignment and the conditional operator associate right to left. Microsoft’s C# reference notes another important rule: operand expressions are generally evaluated from left to right even though operator precedence determines how the results are grouped.
Compile and Run
dotnet new console -n OperatorLab
cd OperatorLab
dotnet build
dotnet run
Use one small example at a time in Program.cs. Predict the result before running the program, then compare your prediction with the actual output. That habit is one of the fastest ways to build intuition for precedence, short-circuiting, and integer division.
Mini Lab
- Create two integer variables and print their sum, difference, product, quotient, and remainder.
- Use at least three compound-assignment operators to update one of the variables.
- Create two comparison expressions and store their results in
boolvariables. - Combine two Boolean expressions with
&&, then combine two with||. - Create one prefix and one postfix increment example and print both the variable and expression result.
- Use
?:to convert a temperature comparison into the string"hot"or"normal". - Create a nullable string and use
??to provide a fallback. - Write one expression whose result changes when parentheses are added, then explain why.
Knowledge Check
- What does the remainder operator
%return? - Why does
17 / 5produce3when both operands are integers? - What is the difference between
=and==? - What does
&&mean? - What does short-circuiting mean for
&&and||? - How do prefix and postfix
++differ inside a larger expression? - What problem does
??solve? - Why are parentheses useful even when you know C# precedence rules?
Answers
- The remainder left after division.
- Integer division discards the fractional portion of the mathematical result.
=assigns;==compares for equality.- Logical AND: both Boolean operands must evaluate to true for the result to be true.
- The right operand may not be evaluated when the left operand already determines the final result.
- Postfix produces the old value before incrementing; prefix increments first and then produces the new value.
- It supplies a fallback when the left operand is null.
- They make grouping explicit, can intentionally override precedence, and improve readability.
Primary References
- Microsoft Learn — C# operators and expressions
- Microsoft Learn — C# expressions overview
- C# Language Specification — Expressions
- Microsoft Learn — Boolean logical operators
- Microsoft Learn — Null-coalescing operators
Elementary Review
C# expressions are built from values and operators. Arithmetic operators calculate, comparison operators return Boolean results, assignment operators update variables, and logical operators combine conditions. Learn the short-circuit behavior of && and ||, be careful with prefix versus postfix increment, and use parentheses whenever they make an expression easier to read. Understanding these rules makes every later lesson on control flow, loops, methods, collections, and object-oriented programming easier.
BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.

Leave a Reply