C#  

Difference Between Record and Class in C# with Examples?

Introduction

When you start working with modern C# development, one question that often comes up is: What is the difference between record and class in C#?

Both record and class are used to create objects, but they are designed for different purposes. Understanding this difference is very important for writing clean, scalable, and maintainable code—especially in real-world applications like APIs, microservices, and enterprise software.

In simple words:

  • A class is used when your object has behavior and can change over time.

  • A record is used when your object represents data and should remain mostly unchanged.

In this article, we will explore everything step by step in simple language with examples so that even beginners can understand easily.

What is a Class in C#?

A class in C# is a blueprint used to create objects that can hold both data and behavior (methods). Classes are a core part of Object-Oriented Programming (OOP).

The most important thing about classes is that they are mutable, which means you can change their values after creating an object.

Example of a Class

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var person1 = new Person { Name = "John", Age = 30 };
person1.Age = 31; // Value changed

Explanation

Here, we created a person object. After creating it, we changed the age from 30 to 31. This is called mutability.

Key Points About Class

  • Data can be changed anytime

  • Used for business logic and operations

  • Supports methods, inheritance, and encapsulation

  • Uses reference equality (compares memory location)

Classes are best when your object needs to do something, not just store data.

What is a Record in C#?

A record in C# is a special type introduced to make working with data models easier and safer. It is mainly used for storing data that should not change after creation.

Records are immutable by design, which means once created, their values are not meant to be modified directly.

Example of a Record

public record Person(string Name, int Age);

var person1 = new Person("John", 30);
var person2 = person1 with { Age = 31 }; // New object created

Explanation

Instead of changing the original object, a new object is created with updated values. The original object remains unchanged.

Key Points About Record

  • Designed for immutable data

  • Uses value-based equality

  • Supports easy copying with with keyword

  • Less code, more readability

Records are best when your object is just data, like API responses or DTOs.

Key Differences Between Record and Class in C#

FeatureClassRecord
Data MutabilityMutable (can change anytime)Immutable (prefer not to change)
EqualityReference-basedValue-based
SyntaxMore code requiredShort and clean
Use CaseLogic + behaviorData representation
CopyingManualBuilt-in (with)

Mutability (Can Data Change?)

In C# classes, you can change property values anytime. This is useful but can sometimes create bugs if data changes unexpectedly.

In C# records, data is mostly fixed. Instead of modifying it, you create a new copy.

// Class
person1.Age = 35;

// Record
var updatedPerson = person1 with { Age = 35 };

Why This Matters

  • Classes → Flexible but risky in large systems

  • Records → Safer and predictable

This is very important in multithreaded applications and APIs.

Equality Comparison (Very Important)

This is one of the biggest differences in record vs class in C#.

Class Behavior

Classes compare memory address.

var p1 = new Person("John", 30);
var p2 = new Person("John", 30);

// False (different objects in memory)

Record Behavior

Records compare actual data values.

// True (same data)

Why This Matters

  • Records are great for data comparison, caching, and testing

  • Classes are better when identity matters (like database entities)

Immutability and Safety

Immutability means data cannot be changed after creation.

Benefits of Records

  • Prevent accidental changes

  • Easier debugging

  • Better thread safety

  • Cleaner code

Classes

Classes do not enforce immutability unless you manually design them.

Syntax Simplicity (Less Code, More Clarity)

Records provide a very clean and short syntax.

// Record
public record Person(string Name, int Age);

// Class
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Why It Matters

  • Less boilerplate code

  • Faster development

  • Easy to read and maintain

Copying Objects

Copying objects is much easier with records.

Class Copy (Manual Work)

var newPerson = new Person
{
    Name = person1.Name,
    Age = person1.Age
};

Record Copy (Simple)

var newPerson = person1 with { Age = 40 };

Why It Matters

This is very useful in state management, APIs, and functional programming styles.

When to Use Class vs Record in C#

Use Class When

  • You need mutable objects

  • Your object contains methods and business logic

  • You are working with domain models or entities

Use Record When

  • You are working with DTOs (Data Transfer Objects)

  • You need immutability

  • You want value-based comparison

  • You want clean and concise code

Real-World Example (Easy to Understand)

Class Example (Changing State)

public class Order
{
    public int Id { get; set; }
    public string Status { get; set; }
}

var order = new Order { Id = 1, Status = "Pending" };
order.Status = "Shipped";

Record Example (Safe Data Model)

public record Order(int Id, string Status);

var order = new Order(1, "Pending");
var updatedOrder = order with { Status = "Shipped" };

Simple Understanding

  • Class → Same object changes

  • Record → New object created with changes

Advantages of Using Records in C#

  • Cleaner and modern syntax

  • Built-in immutability

  • Easy comparison of objects

  • Less code to write

  • Ideal for APIs and microservices

Advantages of Using Classes in C#

  • Full control over behavior and state

  • Supports complex logic

  • Flexible and powerful

  • Widely used in enterprise applications

Summary

Understanding the difference between record and class in C# is essential for writing better and more efficient code. A class is best suited for objects that contain behavior and can change over time, while a record is ideal for representing data that should remain consistent and easy to compare. In modern C# applications, developers often use records for data models like DTOs and classes for business logic. Choosing the right one depends on your use case, but using both wisely can significantly improve code quality, readability, and maintainability.