Struct Vs Class When To Use Which · TL;DR
1 min readRapid overview
TL;DR
| Feature | struct | class |
|---|---|---|
| Type category | Value type | Reference type |
| Memory allocation | Stored inline (stack, or inside another object) | Stored on heap, referenced via pointer |
| Default behavior | Copied by value (creates a full copy) | Copied by reference (points to same object) |
| Nullability | Cannot be null (unless Nullable<T>) | Can be null |
| Inheritance | Cannot inherit from another struct or class; only from ValueType | Supports inheritance and polymorphism |
| Interfaces | Can implement interfaces | Can implement interfaces and base classes |
| Default constructor | Cannot define a custom parameterless constructor (C# 10 adds limited support) | Can freely define constructors |
| Finalizer / Destructor | Not supported | Supported |
| GC behavior | Usually short-lived, reclaimed when out of scope | Managed by the Garbage Collector |
| Boxing / Unboxing | Converting to/from object/interface causes allocation | No boxing/unboxing issues |
| Thread safety | Safer for small immutable data | Reference types require synchronization if shared |