What is the difference between a class and a struct in C#?
Compare classes and structs in C#: reference vs value types, heap vs inline storage, inheritance, nullability, and when to choose each for performance.
Expected Interview Answer
A class is a reference type allocated on the managed heap and copied by reference, while a struct is a value type usually stored inline and copied by value, making structs best for small, immutable, short-lived data.
Classes support inheritance, can be null, and are ideal for larger objects with identity and shared mutable state. Structs cannot inherit from other structs or classes (they only implement interfaces), are non-nullable by default, and copy their whole contents on assignment, which avoids heap allocation and GC pressure but can be costly for large structs. Choosing between them affects performance, equality semantics, and how data flows through your program.
- Structs avoid heap allocation for small data
- Classes enable inheritance and polymorphism
- Value semantics give predictable, independent copies
- Reference semantics allow shared, mutable identity
- Correct choice reduces garbage-collection overhead
AI Mentor Explanation
A struct is like each fielder's personal set of position notes: compact, self-contained, and copied fresh for every player. A class is like the shared team dossier held in the dressing room that everyone references and that can grow chapters over seasons through inheritance, with one master copy updated for all rather than duplicated per person.
Step-by-Step Explanation
Step 1
Classify the type
A class is a reference type; a struct is a value type declared with the struct keyword.
Step 2
Compare storage
Class instances live on the heap; struct values live inline, avoiding a separate allocation.
Step 3
Compare copying
Assigning a class copies the reference; assigning a struct copies the entire value.
Step 4
Check inheritance and nullability
Classes support inheritance and can be null; structs implement interfaces only and are non-nullable by default.
Step 5
Decide by use case
Prefer small, immutable, short-lived data as a struct; use a class for identity, shared state, or larger objects.
What Interviewer Expects
- Knows struct is a value type and class is a reference type
- Explains heap vs inline storage and copy semantics
- Aware structs cannot inherit but can implement interfaces
- Understands nullability differences
- Can give guidance on when to choose each
Common Mistakes
- Saying structs support class inheritance
- Claiming structs are always faster regardless of size
- Forgetting structs are copied by value in method calls
- Believing a struct can never be stored on the heap
Best Answer (HR Friendly)
“A class is a bigger, shareable object that lives in a common memory area, so different variables can point to the same one. A struct is a small, self-contained value that gets copied whenever you assign it, which is efficient for little pieces of data.”
Code Example
struct Coordinate
{
public double Lat;
public double Lng;
}
class Location
{
public string Name = "";
public Coordinate Position;
}
var a = new Coordinate { Lat = 1, Lng = 2 };
var b = a; // copied by value
b.Lat = 9; // a.Lat is still 1Follow-up Questions
- When should you use a struct instead of a class?
- Why are large mutable structs discouraged?
- What is a readonly struct and what does it guarantee?
- Can a struct implement an interface, and what is the cost?
- How do record and record struct differ from class and struct?
MCQ Practice
1. Which statement about structs in C# is correct?
Structs are value types copied by value; they can implement interfaces but cannot inherit from other structs or classes.
2. Which type supports inheritance in C#?
Classes support inheritance and polymorphism; structs only implement interfaces and cannot inherit.
3. When is a struct usually the better choice?
Small, immutable, short-lived data benefits from a struct's value semantics and avoids heap allocation.
Flash Cards
Class vs struct in one line? — A class is a reference type on the heap; a struct is a value type copied by value.
Can a struct inherit? — No, structs cannot inherit from structs or classes, but they can implement interfaces.
Which is null by default, class or struct? — A class reference can be null; a struct is non-nullable by default.
When prefer a struct? — For small, immutable, short-lived data to avoid heap allocation and GC pressure.
When prefer a class? — For larger objects, shared mutable identity, or when you need inheritance.
Continue Learning
Related Interview Questions
What is the difference between a value type and a reference type in C#?
medium
What are the boxing and unboxing operations in C# and why do they matter?
medium
What are records in C# and how do they differ from classes?
medium
When does using a struct hurt performance, and what do readonly struct, ref struct and in parameters fix?
hard