What is the difference between a class and a struct in C#?
Answer
Class: a reference type — instances are allocated on the heap, and variables hold references. Supports inheritance, can have a finalizer (~ClassName()), default value is null. Best for: complex objects with identity, mutable state, and when inheritance is needed. Struct: a value type — instances are typically on the stack (or inline). Cannot inherit from other structs/classes (but can implement interfaces). No finalizer. Default value is all-zeros (not null). Best for: small, immutable data groupings where copying is cheap (Vector3, DateTime, Point). Key guidelines: use struct when: the object is ≤ 16 bytes, immutable, logically a value, and won't be boxed frequently. Record struct (C# 10+) provides value-semantic records. Mutable large structs are an antipattern — they get copied on every assignment.
Previous
What is the difference between value types and reference types in C#?
Next
What are access modifiers in C#?