In C#, both classes and records are used to represent complex data structures, but they have distinct purposes, features, and usage patterns. Below is a comparison between them:
1. Class in C#
Overview
- A class is a reference type that represents an object with properties, fields, and methods.
- It allows both mutability (ability to change object state) and encapsulation (hiding internal state).
Key Features
- Supports inheritance and polymorphism.
- Encapsulates behavior using methods and fields.
- State can change over time (mutable).
- Two objects of the same class with identical values are not equal unless you override the equality members (like
Equals
andGetHashCode
).
Example of a Class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString() => $"Name: {Name}, Age: {Age}";
}
C#When to Use?
- When the focus is on behavior along with data.
- When you need inheritance and polymorphism.
- When the object’s state will change frequently.
2. Record in C# (Introduced in C# 9.0)
Overview
- A record is a special type of class optimized for immutable data.
- It is typically used to represent data models where the data does not change after initialization (although mutable records are allowed).
Key Features
- Records are reference types like classes.
- Automatically generates value-based equality (
==
,Equals
,GetHashCode
). - Supports deconstruction into individual components.
- The primary intent is immutable data structures (by default).
Example of a Record:
public record Person(string Name, int Age);
C#With a record, two instances with the same values will be treated as equal:
var person1 = new Person("Alice", 30);
var person2 = new Person("Alice", 30);
Console.WriteLine(person1 == person2); // True
C#When to Use?
- When the focus is primarily on data rather than behavior.
- When immutable objects are preferred.
- For data transfer objects (DTOs) or state snapshots.
3. Class vs Record Comparison Table
Feature | Class | Record |
---|---|---|
Type | Reference type | Reference type |
Mutability | Mutable | Immutable by default |
Equality | Reference-based equality (unless overridden) | Value-based equality by default |
Inheritance | Supports inheritance | Supports inheritance |
Deconstruction | Not available by default | Supports deconstruction syntax |
Use Case | When behavior is as important as data | When the focus is primarily on data |
Syntax | class Person {} | record Person {} or record Person(...) |
4. Record Class and Record Struct
- Record Class: By default, a record is a class (reference type).
- Record Struct: Introduced in C# 10, it is a value type version of records for scenarios where copying data is more efficient.
Example of Record Struct:
public record struct Point(int X, int Y);
C#5. Conclusion
- Use classes when you need mutable objects, inheritance, or objects with behavior.
- Use records when you want immutable, value-based objects primarily used to hold data (like DTOs). Records simplify code where equality and immutability are important.