struct vs class (real-world)?
struct vs class (real-world)
What interviewers test
- Memory & performance awareness
Use struct when:
- Small
- Immutable
- Value-type behavior
public readonly struct Point
{
public int X { get; }
public int Y { get; }
}
Use class when:
- Large
- Mutable
- Shared references
public class User
{
public string Name { get; set; }
}
Interview statement
“Structs live on stack or inline, classes live on heap with GC cost.”