C# and .NET • Modern C# baseline
Operators, Conversions, and Expressions
Understand integer division, precedence, and checked conversions.
C# evaluates expressions according to operand types and operator precedence. Integer division discards the fractional portion. Checked arithmetic can detect overflow for supported integral operations; unchecked arithmetic may wrap for unsigned types and has different behavior for signed types. Use parentheses when they improve reviewability.
Key points
- Use explicit conversions when a possible loss is understood.
- Checked contexts are useful at boundaries where overflow must be reported.
Example
int whole = 17 / 5;
double fractional = 17.0 / 5.0;
int scaled = checked(200 * 3);
Console.WriteLine($"{whole} {fractional} {scaled}");
What to expect
3 3.4 600
Practice exercise
Convert a user-provided long to int safely and handle values outside the int range.
Key idea: Make overflow and narrowing behavior a deliberate part of the input contract.