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.

⭐ Quick Memory Trick:
Class → "WHO is this?" (Identity)
Struct → "WHAT value is this?" (Value)
Record → "Is this data equal to that data?" (Value-based equality)

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


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; }
}
⭐ Interview Point: Struct means value type. It does not mean immutable.

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

ClassRecord
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)
⭐ Non-destructive mutation: "Create a copy, change selected properties, return new object."

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

TypeStorageEqualitywith
recordReferenceValue-based✅
record structValueValue-based✅
readonly record structValue (immutable)Value-based✅

10. Four Important Type Categories

C# Types ├── Reference Types │ ├── class │ └── record └── Value Types ├── struct └── record struct

11. Class vs Struct

FeatureClassStruct
TypeReferenceValue
AssignmentCopies referenceCopies value
IdentityUsually importantUsually not
InheritanceSupports class inheritanceCannot inherit
Typical useEntitiesSmall values

12. Class vs Record

FeatureClassRecord
Main conceptIdentityData
EqualityIdentity-orientedValue-based
with❌✅
Good forEntitiesDTOs, messages, events

13. Choosing the Right Type

Does identity matter? → YES → class (Customer, Order, BankAccount) Is it primarily data where equality matters? → YES → record (DTO, API response, Event, Message) Is it a small value with no identity? → YES → struct (Point, Coordinates, Temperature)
⭐ Rule of thumb: Choose struct when data is value-oriented and short-lived, and when identity does not matter.
Is it a small value + record semantics? → record struct (Small value objects)

14. Memory: Don't Say "Struct = Stack"

⭐ Interview Answer: Don't say "Structs are stored on the stack and classes on the heap." The correct concept is value type vs reference type. Actual storage depends on context.

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

ConceptRemember
ClassReference type, identity matters
StructValue type, value matters
RecordReference type, value-based equality
Record StructValue type, value-based equality
withNon-destructive mutation (records only)
BoxingValue type → reference type

Easy Memory Tricks

Class → WHO? (Identity) Struct → WHAT? (Value) Record → DATA (Value-based equality) readonly struct → Value + Cannot change record struct → Value + Record semantics

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.