Understanding Classes and Records in C#
Introduction
In C#, classes are fundamental building blocks of object-oriented programming (OOP). They allow you to define blueprints for creating objects that encapsulate data and behavior. With the introduction of C# 9.0, the language also introduced a new feature called “records”
In this blog post, we’ll explore the concepts of classes and records in C# and provide practical implementations for both.
Classes in C#
Definition:
A class is a user-defined type that represents a blueprint for creating objects. Objects created from a class are instances of that class, and they encapsulate both data (in the form of fields or properties) and behavior (in the form of methods).
Example:
Let’s create a simple Person
class to represent individuals with a name and age:
public class Person
{
// Fields
private string name;
private int age;
// Constructor
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
// Properties
public string Name => name;
public int Age => age;
// Method
public void PrintDetails()
{
Console.WriteLine($"Name: {name}, Age: {age}");
}
}
Usage:
Now, let’s use the Person
class:
class Program
{
static void Main()
{
// Create an instance of Person
Person person1 = new Person("John Doe", 30);
// Access properties and invoke method
Console.WriteLine($"Name: {person1.Name}, Age: {person1.Age}");
person1.PrintDetails();
}
}
Records in C# (C# 9.0 and later)
Definition:
A record is a new reference type introduced in C# 9.0 that provides a concise syntax for declaring immutable classes.
Example:
Let’s create a PersonRecord
using the record syntax:
public record PersonRecord(string Name, int Age);
In this single line, we declare a read-only property for each field and the compiler generates a constructor.
Usage:
Now, let’s use the PersonRecord
:
class Program
{
static void Main()
{
// Create an instance of PersonRecord
PersonRecord person2 = new PersonRecord("Jane Doe", 25);
// Access properties (no need for getters)
Console.WriteLine($"Name: {person2.Name}, Age: {person2.Age}");
}
}
Key Differences:
- Mutability:
- Class: Classes can be mutable or immutable. You can control mutability by defining properties with or without setters.
- Record: Records are inherently immutable. Once created, the values of a record cannot be changed.
2. Equality:
- Class: Equality must be explicitly defined by overriding the
Equals
method and providing appropriate implementations forGetHashCode
and the==
operator. - Record: Equality is provided by default. Records automatically generate meaningful equality members based on their properties.
3. Conciseness:
- Class: Requires more boilerplate code for property declarations, constructors, equality members, etc.
- Record: Provides a concise syntax for declaring immutable classes, reducing the amount of boilerplate code.
Conclusion
Classes and records are both powerful constructs in C#, offering different approaches to modeling data and behavior. Classes provide flexibility in terms of mutability and customization, while records offer conciseness and immutability with built-in support for equality.
The choice between classes and records depends on the specific requirements of your application. For data-centric scenarios where immutability and equality are crucial, records can be a more expressive and maintainable choice. However, for more complex scenarios with custom behavior and mutable state, classes may still be the preferred option.
Remember that records are available from C# 9.0 onwards, so make sure to use an appropriate version of the language to take advantage of this feature.
For a hands-on experience and practical implementation of the concepts discussed in this blog, explore the [demo repository]. This repository contains sample code and examples showcasing the use of classes and records in C#.