Value vs Reference Types, Nullables & Structs Masterclass

โšก C# 12 & .NET 8 ๐ŸŸข Chapter 5 of 35 ๐Ÿ“‚ Phase 2: Data Types ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: Value vs Reference Types ยท Stack vs Heap ยท Primitive Data Types ยท float vs double vs decimal ยท DateTime ยท Guid ยท Nullable Types (T?) ยท Null-Coalescing (??) ยท sizeof ยท Default Values

Welcome to Phase 2 (Chapter 5): C# Data Types, Value vs Reference Types, Nullables & Structs Masterclass! Data types inform the compiler how much memory to allocate and what operations are valid. C# divides all data types into two fundamental categories: Value Types (stored directly on the Stack) and Reference Types (stored on the Heap).

1Value Types vs Reference Types โ€” Stack vs Heap Memory
Stack vs Heap Memory Model in C#: STACK MEMORY (Fast, Fixed Size) HEAP MEMORY (Flexible, Managed by GC) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ int age = 21 โ”‚ โ”‚ โ”‚ โ”‚ decimal salary = 45000.50m โ”‚ โ”‚ โ”‚ โ”‚ bool isActive = true โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ string nameRef โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ "Ravi" (string object) โ”‚ โ”‚ int[] numbersRef โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ [ 10, 20, 30, 40 ] (array object) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
PropertyValue TypesReference Types
Storage LocationStored directly on the StackReference pointer on Stack, actual object on Heap
Assignment BehaviorCopies the actual valueCopies the reference / memory pointer
Default ValueZero / False / Empty structnull
NullabilityCannot be null by default (use T?)Can be null
Examplesint, long, float, double, decimal, char, bool, struct, enumstring, object, class, array, delegate, interface
2Built-In Primitive Data Types
Type.NET TypeSizeRange / PrecisionSuffix
byteSystem.Byte1 byte (8 bits)0 to 255โ€“
shortSystem.Int162 bytes (16 bits)-32,768 to 32,767โ€“
intSystem.Int324 bytes (32 bits)-2,147,483,648 to 2,147,483,647โ€“
longSystem.Int648 bytes (64 bits)-9.2ร—10ยนโธ to 9.2ร—10ยนโธL
floatSystem.Single4 bytes (32 bits)7 digits precisionf
doubleSystem.Double8 bytes (64 bits)15-17 digits precisiond
decimalSystem.Decimal16 bytes (128 bits)28-29 digits precision (Exact for Money)m
charSystem.Char2 bytes (16 bits)Single UTF-16 Unicode character' '
boolSystem.Boolean1 bytetrue or falseโ€“
stringSystem.StringReferenceUnicode text sequence" "
C# โ€” Primitive Types Code Example โ–ถ Run in Compiler
int count = 10;
long population = 8000000000L;
float temp = 36.6f;
double distance = 149597870.7;
decimal salary = 45000.50m; // Use decimal for money & finance!
char grade = 'A';
bool isActive = true;
string course = "C# Masterclass";

Console.WriteLine($"Count: {count}, Salary: {salary:C}, Course: {course}");
Console.WriteLine($"sizeof(int): {sizeof(int)} bytes, sizeof(double): {sizeof(double)} bytes");
3DateTime, Guid & Nullable Types (T?)
C# โ€” DateTime, Guid, Nullable Types โ–ถ Run in Compiler
// 1. DateTime
DateTime now = DateTime.Now;
Console.WriteLine($"Current Time: {now:yyyy-MM-dd HH:mm:ss}");

// 2. Guid (Globally Unique Identifier)
Guid id = Guid.NewGuid();
Console.WriteLine($"Generated GUID: {id}");

// 3. Nullable Types (T?)
int? optionalAge = null;
int finalAge = optionalAge ?? 18; // Null-coalescing fallback
Console.WriteLine($"Final Age: {finalAge}");
4Technical FAQs

Q1: Why should I use decimal instead of double for financial calculations?

double and float use base-2 binary floating-point representation, causing tiny rounding errors (e.g., 0.1 + 0.2 = 0.30000000000000004). decimal uses base-10 representation with 28-29 digits of exact precision, preventing monetary rounding bugs.