| by Arround The Web | No comments

How to Differentiate Between C# Records and Classes

C# is a powerful and a very extensive feature that covers a wide array of usage. This includes threading, structs, and many more. However, despite its diversity, you will often find yourself needing to create custom types to fit your application.

In the recent C# version, we have two methods of defining the custom types: records and classes. Although they are fundamentally different, they can seem very similar and confusing to differentiate.

In this tutorial, we will explore the differences between a record and a class in C# and cover in which scenario you might prefer to use one over the other.

C# Classes

The most common and foundational component of C# and any other OOP language is a class. A class refers to a layout or as commonly called as a blueprint for creating objects. A class encapsulates the data and behavior of an object using the components such as properties and methods.

A class supports the inheritance and act as reference types. This means that once we create an object from a given class, that object reference is passed around rather than the actual object.

The following shows an example of a basic class in C#:

public class Car
{
    public string Model { get; set; }
    public string Year { get; set; }

    public Car(string model, string year)
    {
        Model = model;
        Year = year;
    }

    public void Start()
    {
        Console.WriteLine($"Model: {Model} {Year}");
    }
}

In the given example, we define a class called “Car” with two properties: Model and Year. We also define a “constructor” method and a “Start” method which print the properties to the console.

As you might notice, the “Car” class holds the data, i.e. properties and behavior, methods.

C# Records

Records, on the other hand, were introduced in C# version 9.0 and above. The use of a record is to define a reference type that can provide a built-in functionality for data encapsulation.

It is basically a way of creating an immutable refence type in a group format. By default, the records provide a value-based equality and non-destructible copy feature.

The following example shows a basic record type in C#:

public record Car(string Model, string Year);

As you might notice, a record is more precise and minimal compared to a class. This is because a record automatically provides a “constructor” method to initialize an object. It also automatically provides implementation for equality and a method to convert the values to a string.

Difference Between Class and Record

There are two main differences between a class and a record:

  1. Immutability
  2. Equality

Immutability

By default, classes are mutable which allows you to modify the values of the supported properties once we create an object from the class.

Records, on the other hand, are immutable by default. Hence, once you create a record object, you cannot change its values.

Equality

When it comes to equality, classes use the reference based equality. Hence, if two objects are equal by reference, they are both similar.

Records, on the other hand, use value-based equality. Hence, if two object records have the same value, they are considered equal.

Conclusion

In this tutorial, we explored the difference between a class and a record in C#. Consider the documentation for the feature of each type.

Share Button

Source: linuxhint.com

Leave a Reply