Records vs Classes vs Structs
C# / .NET Interview Notes
1. Big Picture
C# provides several ways to define custom types: class, struct, record, and record struct.
2. Class
A class is a reference type.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person p1 = new() { Name = "John", Age = 30 };
Person p2 = p1;
p2.Name = "David";
Console.WriteLine(p1.Name); // David (same object)
Typical use
- Customer, Employee, Order
- BankAccount, Vehicle
- Service, Repository
3. Struct
A struct is a value type.
struct Point
{
public int X;
public int Y;
}
Point p1 = new() { X = 10, Y = 20 };
Point p2 = p1;
p2.X = 100;
Console.WriteLine(p1.X); // 10 (copy, not same object)
4. Important: Are Structs Mutable?
Yes. A struct can be either mutable or immutable.
// Mutable struct
struct Point
{
public int X;
public int Y;
}
// Immutable struct
readonly struct Point
{
public int X { get; }
public int Y { get; }
}
5. Record
A record is designed for data-oriented types with value-based equality.
record Person(string Name, int Age);
Person p1 = new("John", 30);
Person p2 = new("John", 30);
Console.WriteLine(p1 == p2); // True (value-based equality)
6. Class Equality vs Record Equality
| Class | Record |
|---|---|
| Reference equality (identity) | Value equality (data) |
p1 == p2 → False (different objects) | p1 == p2 → True (same data) |
7. Records and Immutability
Records encourage immutable patterns with with expressions:
Person p1 = new("John", 30);
Person p2 = p1 with { Name = "David" };
// p1 → John, 30 (unchanged)
// p2 → David, 30 (new copy)
8. Record Does NOT Automatically Mean Immutable
record Person
{
public string Name { get; set; } // Mutable!
}
var person = new Person();
person.Name = "John"; // Allowed
9. Record Class vs Record Struct
| Type | Storage | Equality | with |
|---|---|---|---|
record | Reference | Value-based | ✅ |
record struct | Value | Value-based | ✅ |
readonly record struct | Value (immutable) | Value-based | ✅ |
10. Four Important Type Categories
11. Class vs Struct
| Feature | Class | Struct |
|---|---|---|
| Type | Reference | Value |
| Assignment | Copies reference | Copies value |
| Identity | Usually important | Usually not |
| Inheritance | Supports class inheritance | Cannot inherit |
| Typical use | Entities | Small values |
12. Class vs Record
| Feature | Class | Record |
|---|---|---|
| Main concept | Identity | Data |
| Equality | Identity-oriented | Value-based |
with | ❌ | ✅ |
| Good for | Entities | DTOs, messages, events |
13. Choosing the Right Type
struct when data is value-oriented and short-lived, and when identity does not matter.
14. Memory: Don't Say "Struct = Stack"
15. Boxing
int x = 10;
object obj = x; // Boxing
int y = (int)obj; // Unboxing
16. Real-World Example
// Order - has identity
class Order
{
public long Id { get; set; }
public decimal Quantity { get; set; }
}
// API response - data-oriented
record OrderResponse(long Id, decimal Quantity, string Status);
// Price - small value
readonly record struct Price(decimal Value);
17. Interview Questions
Q1. Is a struct mutable?
Answer: Yes. A struct describes a value type, not immutability.
Q2. Are records immutable?
Answer: Records are designed for immutable patterns, but aren't inherently immutable. A record can contain mutable properties.
Q3. Is a record a value type?
Answer: A normal record is a reference type. record struct is a value type.
Q4. Why use records for DTOs?
Answer: DTOs primarily represent data rather than object identity, so records' value-based equality and concise syntax make them a natural choice.
Q5. Are structs always stored on the stack?
Answer: No. Structs are value types, but their storage location depends on context.
18. Quick Revision
| Concept | Remember |
|---|---|
| Class | Reference type, identity matters |
| Struct | Value type, value matters |
| Record | Reference type, value-based equality |
| Record Struct | Value type, value-based equality |
with | Non-destructive mutation (records only) |
| Boxing | Value type → reference type |
Easy Memory Tricks
19. One-Line Interview Answer
A class is a reference type typically used for identity-based entities, a struct is a value type typically used for small values, and a record is a data-oriented type that provides value-based equality and convenient immutable-style operations such as with.