C# Mastery

The Precision of Numbers: Checked vs Unchecked Math

1 Views Updated 5/4/2026

The Precision of Numbers

In high-frequency systems, math isn't just about addition; it's about Precision and Overflow. If you add 1 to the largest possible int, C# will "Wrap Around" to a massive negative number without telling you. This silent bug can destroy financial or healthcare systems.

1. Integral Types: From SByte to ULong

Choosing the right size matters. A byte (0-255) consumes a fraction of the memory of a long.

Type Bytes Range Example
byte10 to 255
short2+/- 32,000
int4+/- 2 Billion
long8Astronomical

2. The "Checked" Keyword (Safety First)

By default, C# math is Unchecked. If you want a crash (OverflowException) instead of a silent wrap-around, you must use the checked block.

int val = int.MaxValue;

// ❌ SILENT BUG: becomes -2,147,483,648
val = val + 1; 

// ✅ THROWS EXCEPTION: Your app crashes safely so you can fix the logic!
checked 
{
    val = val + 1; 
}

3. Floating Point vs Decimal

NEVER use double for money. Double uses binary approximation and suffers from "Rounding Errors." Always use decimal for precision-critical data.

double d = 0.1 + 0.2; // Might result in 0.30000000000004
decimal m = 0.1m + 0.2m; // Exactly 0.3m

4. Interview Mastery

Q: "Why is decimal slower than double or float if it's more accurate?"

Architect Answer: "Float and Double are 'Native' types. Modern CPUs have physical circuits (FPUs) designed specifically to do binary floating-point math at the speed of light. `Decimal`, on the other hand, is not a native CPU type; it is a 128-bit structure managed by the C# runtime. Every addition or multiplication on a decimal requires several CPU operations and manual normalization by the CLR. We trade incredible raw speed for absolute decimal precision, which is non-negotiable in financial engineering."

C# Mastery
1. Modern C# & Framework Fundamentals
Introduction to the .NET Ecosystem & Runtime (JIT, CLR) C# 12/13 Top-Level Statements & Global Usings Variables, Data Types, and Value vs Reference Deep Dive Mastering Nullable Reference Types & Null Safety The Magic of Strings (Interpolation, Verbatim, and Immutability)
2. Control Flow & Logical Structures
Advanced Pattern Matching (Switch Expressions) The Precision of Numbers: Checked vs Unchecked Math Iterators: Foreach, Yield Return, and Deferred Execution Defensive Programming: Guard Clauses and Exception Mastery
3. Object-Oriented Mastery
Classes vs Structs vs Records (Which to use when?) Mastering Primary Constructors & Object Initializers Interface Architectures: Default Implementations & Segregation Polymorphism vs Composition (The Architect's Dilemma) Partial Classes, Extension Methods, and Static Classes
4. Functional C# & Collections
Delegates, Func, Action, and Predicates Lambda Expressions & The Evolution of Linq Dynamic Array Management: List<T>, Dictionary, and HashSet Custom Collections & Yielding Data streams
5. Asynchronous & Parallel Programming
Async/Await Deep Dive: Task Lifecycle & Thread Safety Synchronization Context & Avoiding Deadlocks Parallel.ForEach vs PLINQ vs Tasks CancellationToken Mastery: Surgically Aborting Operations
6. Advanced Engineering & High Performance
Generics & Constraints: Building Type-Safe Libraries Reflection and Attributes: Reading Metadata at Runtime Dependency Injection Internals (ServiceCollection from scratch) High-Performance Memory: Span<T> and ReadOnlySpan<T> Garbage Collection Segments & LOH Internals Managing Unmanaged Resources: IDisposable & Finalizers Introduction to Source Generators & Interceptors C# Interview Masterclass: Architect-Level Explanations