Records in C# (Interview Guide)
Records in C# are reference types (by default) designed for immutable data models with value-based equality.
They were introduced in C# 9 and improved in C# 10 & C# 11.
1. Why Records? (Interview Answer)
Before Records, we used classes to store data:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Problem with Classes:
-
Mutable (data can change)
-
Reference equality (not value equality)
-
More boilerplate code
Records solve this by:
-
Immutable by default
-
Value-based equality
-
Less code
-
Built-in
ToString(),Equals(),GetHashCode()
2. Basic Record Example
public record Person(string Name, int Age);
Usage:
var person1 = new Person("John", 30);
var person2 = new Person("John", 30);
Console.WriteLine(person1 == person2);
Output:
True
👉 Because Records compare values, not references.
3. Record vs Class (Important Interview Question)
Class Example
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
var p1 = new Person { Name = "John", Age = 30 };
var p2 = new Person { Name = "John", Age = 30 };
Console.WriteLine(p1 == p2);
Output:
False
Record Example
public record Person(string Name, int Age);
var p1 = new Person("John", 30);
var p2 = new Person("John", 30);
Console.WriteLine(p1 == p2);
Output:
True
4. Immutability in Records 🔒
Records are immutable by default
public record Person(string Name, int Age);
var person = new Person("John", 30);
// person.Name = "David"; ❌ Error
5. "with" Keyword (Very Important Interview Question)
Used to create a copy with modification
public record Person(string Name, int Age);
var person1 = new Person("John", 30);
var person2 = person1 with { Age = 35 };
Console.WriteLine(person1);
Console.WriteLine(person2);
Output:
Person { Name = John, Age = 30 }
Person { Name = John, Age = 35 }
👉 This is called Non-destructive mutation
6. Record with Properties
public record Person
{
public string Name { get; init; }
public int Age { get; init; }
}
Usage:
var person = new Person
{
Name = "John",
Age = 30
};
👉 init allows setting only during initialization
7. Record Inheritance
public record Person(string Name);
public record Employee(string Name, int Salary) : Person(Name);
Usage:
var emp = new Employee("John", 50000);
Console.WriteLine(emp);
8. Record Struct (C# 10)
Records can also be structs
public record struct Person(string Name, int Age);
Difference:
| Type | Memory |
|---|---|
| record | Reference Type |
| record struct | Value Type |
9. Positional Records
public record Person(string Name, int Age);
Equivalent to:
public record Person
{
public string Name { get; init; }
public int Age { get; init; }
public Person(string Name, int Age)
{
this.Name = Name;
this.Age = Age;
}
}
10. Deconstruction (Interview Favorite)
public record Person(string Name, int Age);
var person = new Person("John", 30);
var (name, age) = person;
Console.WriteLine(name);
Console.WriteLine(age);
11. When to Use Records (Interview Answer) ✅
Use Records when:
✔ Data models
✔ DTOs
✔ Immutable objects
✔ Value comparison needed
✔ Read-only objects
Example:
public record EmployeeDto(int Id, string Name);
12. When NOT to Use Records ❌
Avoid Records when:
❌ Mutable objects needed
❌ Heavy business logic
❌ Entity Framework entities (sometimes not recommended)
13. Real-World Example (Interview Ready)
public record Order(int Id, decimal Amount);
var order1 = new Order(1, 500);
var order2 = new Order(1, 500);
Console.WriteLine(order1 == order2);
Output:
True
14. Records vs Struct vs Class (Interview Table)
| Feature | Class | Struct | Record |
|---|---|---|---|
| Type | Reference | Value | Reference |
| Immutable | No | No | Yes |
| Equality | Reference | Value | Value |
| Boilerplate | More | Less | Very Less |
| Use Case | Business Logic | Small Data | DTO/Data Models |
15. Tricky Interview Questions
Q1: Are Records immutable?
👉 Yes, by default (using init)
Q2: Are Records reference types?
👉 Yes (unless using record struct)
Q3: Can Records inherit?
👉 Yes
Q4: Can Records have methods?
👉 Yes
public record Person(string Name, int Age)
{
public void Print()
{
Console.WriteLine(Name);
}
}
Q5: Can we modify record values?
👉 Yes using with
var p2 = p1 with { Age = 40 };
16. Most Important Interview One-Liner
Records are immutable reference types used for storing data with value-based equality.
