Value vs Reference Types, Nullables & Structs Masterclass
๐ 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) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Property | Value Types | Reference Types |
|---|---|---|
| Storage Location | Stored directly on the Stack | Reference pointer on Stack, actual object on Heap |
| Assignment Behavior | Copies the actual value | Copies the reference / memory pointer |
| Default Value | Zero / False / Empty struct | null |
| Nullability | Cannot be null by default (use T?) | Can be null |
| Examples | int, long, float, double, decimal, char, bool, struct, enum | string, object, class, array, delegate, interface |
2Built-In Primitive Data Types
| Type | .NET Type | Size | Range / Precision | Suffix |
|---|---|---|---|---|
byte | System.Byte | 1 byte (8 bits) | 0 to 255 | โ |
short | System.Int16 | 2 bytes (16 bits) | -32,768 to 32,767 | โ |
int | System.Int32 | 4 bytes (32 bits) | -2,147,483,648 to 2,147,483,647 | โ |
long | System.Int64 | 8 bytes (64 bits) | -9.2ร10ยนโธ to 9.2ร10ยนโธ | L |
float | System.Single | 4 bytes (32 bits) | 7 digits precision | f |
double | System.Double | 8 bytes (64 bits) | 15-17 digits precision | d |
decimal | System.Decimal | 16 bytes (128 bits) | 28-29 digits precision (Exact for Money) | m |
char | System.Char | 2 bytes (16 bits) | Single UTF-16 Unicode character | ' ' |
bool | System.Boolean | 1 byte | true or false | โ |
string | System.String | Reference | Unicode 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.