Class vs Record vs Struct in C#: Understanding the Differences
Explore how Class, Record, and Struct differ in C# and learn when to use each for better performance and design.
C# has evolved a lot over the years, and with C# 9 and 10, records and improved structs bring new options to model your data. Choosing the right type is essential for performance, maintainability, and clarity of your code. Let’s dive into the differences between Class, Record, and Struct in C# with examples.
1. Class
A class in C# is a reference type, meaning it is stored on the heap, and assignments create references to the same object. Classes support inheritance, encapsulation, and polymorphism.
Example
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
// Usage
var person1 = new Person("Alice", 25);
var person2 = person1;
person2.Age = 30;
Console.WriteLine(person1.Age); // Output: 30Key Points:
Reference type → multiple variables can reference the same object.
Mutable by default.
Supports inheritance and polymorphism.
Good for large, complex objects with behavior.
2. Record
A record is a reference type designed for immutable data models. Records are great for data-centric objects, like DTOs or configuration settings, and support value-based equality out of the box.
Example
public record PersonRecord(string Name, int Age);
// Usage
var record1 = new PersonRecord("Bob", 30);
var record2 = record1 with { Age = 35 };
Console.WriteLine(record1.Age); // Output: 30
Console.WriteLine(record2.Age); // Output: 35Key Points:
Immutable by default (though you can define mutable properties).
Supports value equality (
EqualsandGetHashCodebased on properties).Ideal for immutable DTOs, event data, or simple models.
Supports
withexpressions for easy copying with changes.
3. Struct
A struct in C# is a value type, stored on the stack (or inline in arrays). Assignments copy the value, making structs lightweight and suitable for performance-critical scenarios.
public struct PersonStruct
{
    public string Name { get; set; }
    public int Age { get; set; }
    public PersonStruct(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
// Usage
var struct1 = new PersonStruct("Charlie", 28);
var struct2 = struct1;
struct2.Age = 35;
Console.WriteLine(struct1.Age); // Output: 28Key Points:
Value type → copying creates a new independent object.
Lightweight, good for small data structures.
Cannot have inheritance.
Best for performance-sensitive, small, immutable objects.
When to Use What?
Choosing between class, record, and struct depends on the nature of your data and how you intend to use it. Use a class when you have complex objects that are mutable, require identity, or need behavior such as methods, inheritance, or polymorphism. Classes are great for large, behavior-rich entities but come with heap allocation and garbage collection overhead.
Use a record when your focus is on immutable data and value-based equality. Records are ideal for data transfer objects (DTOs), configuration settings, or event data where the content matters more than the object identity. They offer concise syntax and built-in equality comparison, making it easy to work with immutable models.
Use a struct for small, lightweight objects where performance is critical. Structs are value types and are stored on the stack, so copying creates independent copies without heap allocation. They are perfect for small data structures such as points, coordinates, or other numeric containers. However, avoid large structs or structs that require inheritance, as this can lead to performance issues or unnecessary complexity.
Rule of thumb: choose a class for objects with behavior, a record for immutable data, and a struct for small, high-performance value objects.
Conclusion
Understanding the difference between class, record, and struct helps you write cleaner, efficient, and maintainable C# code. Choosing the right type depends on mutability, size, and identity requirements of your objects.
Class → mutable, identity-focused, behavior-rich.
Record → immutable, data-centric, value-based equality.
Struct → lightweight, value-focused, high-performance.
Choosing wisely can improve performance, readability, and reduce subtle bugs related to object copying or equality.
🔹 Example Summary
Person person = new("Alice", 25);          // Class
PersonRecord record = new("Bob", 30);      // Record
PersonStruct pStruct = new("Charlie", 28); // Struct💡 Question for you: Which one do you find yourself using most often in your C# projects, and why?
👉 Full working code available at:
🔗https://sourcecode.kanaiyakatarmal.com/ClassvsRecordvsStruct
I hope you found this guide helpful and informative.
Thanks for reading!
If you enjoyed this article, feel free to share it and follow me for more practical, developer-friendly content like this.


