C# Operators
Operators are symbols that perform operations on values (called operands). C# groups them into arithmetic, assignment, comparison, logical and a few special operators.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 2 |
9 |
- |
Subtraction | 7 - 2 |
5 |
* |
Multiplication | 7 * 2 |
14 |
/ |
Division | 7 / 2 |
3 |
% |
Remainder (modulus) | 7 % 2 |
1 |
++ |
Increment | x++ |
adds 1 |
-- |
Decrement | x-- |
subtracts 1 |
The surprise for beginners is /: when both operands are integers, the result is an integer and the fraction is thrown away. Make one operand a double to get a fractional result.
using System;
Console.WriteLine(7 / 2);
Console.WriteLine(7.0 / 2);
Console.WriteLine((double)7 / 2);
Console.WriteLine(7 % 2);
Console.WriteLine(-7 / 2);
Console.WriteLine(-7 % 2);3
3.5
3.5
1
-3
-1
Integer division truncates toward zero, and the remainder takes the sign of the left operand.
Increment and decrement
x++ (postfix) uses the current value and then adds one; ++x (prefix) adds one first. On a line of their own they behave identically; the difference only shows inside a larger expression.
using System;
int a = 5;
int b = a++; // b gets 5, then a becomes 6
Console.WriteLine(quot;a={a}, b={b}");
int c = 5;
int d = ++c; // c becomes 6, then d gets 6
Console.WriteLine(quot;c={c}, d={d}");a=6, b=5
c=6, d=6
Assignment operators
Compound assignment combines an operation with =:
| Operator | Same as |
|---|---|
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x %= 3 |
x = x % 3 |
x ??= y |
assign y only if x is null |
using System;
int total = 10;
total += 5;
total *= 2;
total -= 4;
total /= 2;
total %= 7;
Console.WriteLine(total);
string label = null;
label ??= "default";
Console.WriteLine(label);6
default
Comparison and logical operators
Comparisons produce a bool. Logical operators combine booleans.
| Operator | Meaning | Operator | Meaning | |
|---|---|---|---|---|
== |
equal to | && |
AND — both true | |
!= |
not equal to | || |
OR — at least one true | |
> / < |
greater / less than | ! |
NOT — inverts | |
>= / <= |
greater/less or equal | ^ |
XOR — exactly one true |
using System;
int age = 20;
bool hasTicket = true;
Console.WriteLine(age >= 18);
Console.WriteLine(age == 21);
Console.WriteLine(age >= 18 && hasTicket);
Console.WriteLine(age < 12 || age > 65);
Console.WriteLine(!hasTicket);True
False
True
False
False
&& and || short-circuit: if the left side already decides the result, the right side is not evaluated. That makes checks like text != null && text.Length > 0 safe.
The conditional and null operators
| Operator | Example | Meaning |
|---|---|---|
?: |
x > 0 ? "pos" : "neg" |
Choose one of two values |
?? |
name ?? "anonymous" |
Use the right side if the left is null |
?. |
text?.Length |
Access a member only if not null |
Operator precedence
Multiplication, division and remainder bind tighter than addition and subtraction; comparisons come next, then &&, then ||. When in doubt, add parentheses — they cost nothing and make intent obvious: (a + b) * c.
Test yourself
What is the value of 17 / 5 in C#?
- 3.4
- 3
- 4
- 2
After int x = 3; int y = x++; what are x and y?
- x=4, y=4
- x=3, y=4
- x=4, y=3
- x=3, y=3
What does 10 % 4 return?
- 2
- 2.5
- 0
- 4