C# Interview Question #79

What is the difference between class, struct, and record in C#?

Memory Management & Modern C# Mid-Level Intermediate

Quick Interview Answer

A class is a reference type and normally uses reference-based equality unless equality is customized. It is appropriate for entities and objects with identity, mutable state, inheritance, or complex behavior.

Detailed Explanation

A class is a reference type and normally uses reference-based equality unless equality is customized. It is appropriate for entities and objects with identity, mutable state, inheritance, or complex behavior.

A struct is a value type. It is best suited to relatively small values with value semantics and should generally be designed carefully to avoid excessive copying.

A record is designed for data-centric value semantics. Record classes are reference types with generated value-based equality, while record structs combine record features with value-type semantics.

The choice should be based on identity, equality semantics, mutability, allocation, and expected usage rather than syntax alone.