Checked and unchecked arithmetic helps you control how your program handles math operations that go beyond the limits of numbers.
Checked and unchecked arithmetic in C Sharp (C#)
checked {
// code where overflow causes an exception
}
unchecked {
// code where overflow is ignored
}checked makes the program throw an error if numbers overflow.
unchecked lets the program continue even if numbers overflow.
int a = int.MaxValue; int b = 1; int c = checked(a + b);
int a = int.MaxValue; int b = 1; int c = unchecked(a + b);
checked {
int x = int.MaxValue;
int y = 1;
int z = x + y; // Overflow exception here
}unchecked {
int x = int.MaxValue;
int y = 1;
int z = x + y; // No error, wraps around
}This program shows how checked throws an error when numbers overflow, while unchecked lets the number wrap around.
using System; class Program { static void Main() { int max = int.MaxValue; try { int resultChecked = checked(max + 1); Console.WriteLine($"Checked result: {resultChecked}"); } catch (OverflowException) { Console.WriteLine("Overflow caught in checked operation!"); } int resultUnchecked = unchecked(max + 1); Console.WriteLine($"Unchecked result: {resultUnchecked}"); } }
By default, arithmetic operations are unchecked unless you enable checked context.
You can enable checked arithmetic for the whole project in project settings if needed.
Use checked blocks when you want to be sure no overflow happens silently.
Use checked to catch overflow errors in math operations.
Use unchecked to ignore overflow and allow wrapping.
Checked and unchecked help you control how your program handles big or small numbers.
Practice
What does the checked keyword do in C# arithmetic operations?
Solution
Step 1: Understand the purpose of
Thecheckedcheckedkeyword is used to detect overflow in arithmetic operations.Step 2: Behavior on overflow
When overflow happens inside acheckedblock, it throws anOverflowException.Final Answer:
It throws an exception if an arithmetic overflow occurs. -> Option CQuick Check:
checked = throws exception on overflow [OK]
- Thinking checked ignores overflow
- Confusing checked with unchecked
- Assuming checked improves speed
Which of the following is the correct syntax to use unchecked for an arithmetic operation in C#?
int result = ?;Solution
Step 1: Recall
Theuncheckedsyntaxuncheckedkeyword can be used as a method-like call:unchecked(expression).Step 2: Identify correct usage
unchecked(2147483647 + 1); usesunchecked(2147483647 + 1);which is valid syntax for unchecked arithmetic.Final Answer:
unchecked(2147483647 + 1); -> Option AQuick Check:
unchecked(expression) is correct syntax [OK]
- Using unchecked without parentheses
- Using checked keyword instead
- Missing braces for block syntax
What is the output of the following C# code?
int max = int.MaxValue;
int result = unchecked(max + 1);
Console.WriteLine(result);Solution
Step 1: Understand unchecked behavior
Usinguncheckedallows overflow to wrap around without exception.Step 2: Calculate overflow wrap
int.MaxValueis 2147483647; adding 1 wraps to -2147483648 (minimum int value).Final Answer:
-2147483648 -> Option DQuick Check:
unchecked wraps overflow to negative min int [OK]
- Expecting exception on unchecked overflow
- Thinking int can hold 2147483648
- Confusing unchecked with checked
Identify the error in this code snippet and fix it:
checked
{
int x = int.MaxValue;
int y = x + 10;
Console.WriteLine(y);
}Solution
Step 1: Analyze checked block behavior
Thecheckedblock throws anOverflowExceptionwhen overflow occurs.Step 2: Identify overflow cause and fix
Adding 10 toint.MaxValuecauses overflow; usinguncheckedblock prevents exception by wrapping.Final Answer:
OverflowException occurs; fix by usinguncheckedblock. -> Option AQuick Check:
checked throws on overflow, unchecked ignores [OK]
- Ignoring overflow exception
- Trying to fix with syntax changes only
- Assuming checked allows overflow silently
You want to safely add two int values a and b and detect overflow. Which code snippet correctly does this?
int a = int.MaxValue;
int b = 1;
int result;
// What to write here?
Console.WriteLine(result);Solution
Step 1: Understand safe addition with overflow detection
Usingcheckeddetects overflow but throws exception; we must catch it.Step 2: Use try-catch with checked
Wrap addition intryblock withcheckedand catchOverflowExceptionto handle overflow safely.Final Answer:
try { result = checked(a + b); } catch (OverflowException) { result = 0; } -> Option BQuick Check:
Use checked with try-catch for safe overflow handling [OK]
- Using unchecked and expecting overflow detection
- Not catching OverflowException
- Ignoring overflow and risking wrong results
Ready to prove it?
Pass the test and this pattern moves unknown
