Hi lovely readers,
Today, we're diving into the world of records in C#—a fairly new addition to the C# language that can simplify your code.
What are Records?
Records were introduced in C# 9. They provide a concise syntax for declaring immutable data types. Think of them as lightweight, immutable classes designed specifically for data storage and retrieval.
Syntax
Let's start by looking at the basic syntax of a record:
public record Person(string FirstName, string LastName);
In this example, we've declared a Person
record with two properties: FirstName
and LastName
. This concise syntax, known as positional record syntax, allows us to define properties directly in the record's constructor parameters.
Now, let's see how easy it is to create a new instance of Person
:
var person = new Person("John", "Doe");
With records, you can create new instances and initialize their properties in a single line, making the code more concise and readable.
Comparison with Classes
Now, let's compare records with a traditional class declaration:
public class PersonClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
In this class declaration, we've defined a PersonClass
with similar properties FirstName
and LastName
. However, unlike records, you need to create a separate body for the class. PersonClass is also mutable, meaning that a value like FirstName in a PersonClass object can be changed after creating the object.
Immutable by Default:
One of the key features of records is immutability. Records are immutable by default, meaning once a record is created, its properties cannot be modified. This promotes safer code by preventing accidental changes to data.
For example:
var person = new Person("John", "Doe"); //record - immutable
var personClass = new PersonClass("John", "Doe"); //class - mutable
person.FirstName = "Dirk"; //not allowed
personClass.FirstName = "Dirk"; //is allowed
Value Equality
Records provide value equality by default, which means two record instances are considered equal if all their properties are equal. This behavior isn’t standard for classes, because classes are only equal based on the location of your heap storage. This makes records great for data processing or DTOs.
Let's see an example:
var person1 = new Person("John", "Doe");
var person2 = new Person("John", "Doe");
var personClass1 = new PersonClass("John", "Doe");
var personClass2 = new PersonClass("John", "Doe");
Console.WriteLine(person1 == person2); // Output: True
Console.WriteLine(personClass1 == personClass2) // Output: False
With Expressions
In addition to their immutability and concise syntax, records in C# also introduce a feature called "with expressions". With expressions allow you to create a new record instance that is a copy of an existing one, with some properties modified.
Let's see how with expressions work with our Person
record:
var person1 = new Person("John", "Doe");
var person2 = person1 with { FirstName = "Jane" };
Console.WriteLine(person1); // Output: Person { FirstName = John, LastName = Doe }
Console.WriteLine(person2); // Output: Person { FirstName = Jane, LastName = Doe }
In this example, we create a new person2
record using a with expression applied to the existing person1
record. We specify the property FirstName
to be modified, setting it to "Jane". The with expression creates a new record instance with the specified property changed while keeping all other properties the same.
With expressions are super useful when you want to create modified copies of existing records without mutating the original ones.
Conclusion
Records in C# are great for defining immutable data types, contrasting with the mutable nature of traditional classes. With their simple syntax and built-in features like value equality and with expressions, records are a powerful tool for managing data in your applications.
I hope this helped! Do you have any questions or comments? Please reach out to me on @lovelacecoding on almost every social media platform. Happy coding!