LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Records vs Classes and Tuples in C#

All three help us group related data, but they behave differently. A class is useful for objects with identity and behaviour. A record is designed for data-focused objects with value-based equality. A tuple is useful when we need to group a few values temporarily without creating a separate type.

1Meet Records, Classes and Tuples

Hi friends! Today we will learn about Records, Classes and Tuples in C#. All three help us store and work with related data, but they behave differently.

A class is useful for objects with identity and behaviour. A record is designed mainly for data with value-based equality. A tuple is useful when we want to group a few values without creating a separate type.

Let's understand their differences with a practical C# program.

2Think About a Student Management App

Imagine a college application storing student details. Every student has an ID and a name.

When two student records have the same ID and name, we may consider them equal. But two student objects can represent different students even if their details match.

For a temporary operation, we might simply group the ID and name together without creating a separate Student type. This is where tuples become useful.

Remember Records compare data, classes represent object identity by default, and tuples group values.

3Explore the Three Approaches

1. What Is a Class?

A class is a blueprint for creating objects. It can contain properties, methods and constructors. Classes are reference types. By default, equality checks whether two variables refer to the same object.

2. What Is a Record?

A record is designed for data-centric objects. A record class provides value-based equality, meaning objects with matching data can be equal. Records also support useful features like with expressions.

3. What Is a Tuple?

A tuple groups multiple values into one object. C# value tuples use ValueTuple and are value types. Named tuple elements make the values easier to understand.

Feature Class Record Tuple
Type Reference Reference by default Value
Equality Reference by default Value-based Value-based
Methods Yes Yes Limited purpose
Inheritance Yes Record inheritance No
Best use Business objects Data models Temporary grouping
Pro Tip Use records for DTOs, classes for objects with behaviour, and tuples for small temporary data groups.
Common Mistake A record class and a record struct are different. Record classes are reference types, while record structs are value types.

4Let's Write the C# Program

Let's create two records, two class objects and one tuple. Then we will compare their behaviour.

Program.csC#
// Create two records
var firstRecord = new StudentRecord(101, "Anu");
var secondRecord = new StudentRecord(101, "Anu");

// Create two class objects
var firstClass = new StudentClass(101, "Anu");
var secondClass = new StudentClass(101, "Anu");

// Create a tuple
var studentTuple = (Id: 101, Name: "Anu");

Console.WriteLine($"Records equal: {firstRecord == secondRecord}");
Console.WriteLine($"Classes equal: {firstClass == secondClass}");
Console.WriteLine($"Tuple: {studentTuple.Id}, {studentTuple.Name}");
Console.WriteLine($"Record: {firstRecord.Name}");

public record StudentRecord(int Id, string Name);

public class StudentClass
{
    public int Id { get; }
    public string Name { get; }

    public StudentClass(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

Here we create two record objects with identical values, two separate class objects and one named tuple.

Remember The == operator on records compares their values. For ordinary classes, it compares references unless equality is overridden.

5Check the Application Result

Run the program using dotnet run. Here is the expected console output.

Output
Records equal: True
Classes equal: False
Tuple: 101, Anu
Record: Anu

Both records contain the same data, so equality returns True.

The class objects are different instances, so equality returns False. The tuple displays its named values, and the record displays the student's name.

6Watch Records, Classes and Tuples in VS Code

Follow the C# code and watch how each statement executes. The corresponding output appears in the terminal as the program runs.

Program.cs - Visual Studio Code
● Program.cs   C#
◫ ⌕ ⑂ ⚙
 
PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL
PS C:\Projects\Demo> dotnet run
 
C# UTF-8   CRLF   C#
Preparing C# application...

7What Should You Remember?

Records, classes and tuples all help us work with related data, but their purpose is different.

Choose classes when you need object identity and business behaviour. Choose records when value-based equality is useful. Choose tuples when you need to group a few values temporarily.

  • Records provide value-based equality.
  • Classes provide reference-based equality by default.
  • Tuples group multiple values conveniently.

8Questions Interviewers Love to Ask

Q1Easy

What is the main difference between a class and a record?

Answer: Classes use reference equality by default. Records provide value-based equality based on their data.

Q2Easy

Are records reference types or value types?

Answer: A record class is a reference type. A record struct is a value type.

Q3Medium

What is the purpose of the with expression?

Answer: It creates a copy of a record with selected property values changed, leaving the original record unchanged.

Q4Medium

Can we use tuples as method return types?

Answer: Yes. A method can return a named tuple containing multiple values without creating a separate custom class.

Q5Hard

Can we inherit one record from another record?

Answer: Yes. Record classes support inheritance from other record classes. A record class cannot inherit from a normal class.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.