A record in C# is a class and struct that provides special syntax and behavior for working with data models. The record modifier instructs the compiler to integrate members which is useful for types whose primary role is storing data. These members include an overload of ToString() and members that support value equality.
Suppose we have a scenario where we use the record in place of class or struct.
For records, value equality means that two variables of a record type are equal if the types match and all property and field values are equal.
An immutable type prevents us from changing any of the properties or field values of an object after it has been instantiated.
csharppublic record Person(string FirstName, string LastName, int Age);
This above code creates a record type named Person with three properties: FirstName, LastName, and Age. The properties are automatically generated as public read-only members.
Here, we use the above-created record type:
csharpvar person1 = new Person("John", "Doe", 30); var person2 = new Person("John", "Doe", 30); Console.WriteLine(person1 == person2); // Output: True (value comparison)
In the following example, we define a public record that uses positional parameters to declare and instantiate a record. It then prints the type name and property values:
csharppublic record Person(string FirstName, string LastName); public static class Program { public static void Main() { Person person = new Person("Aman", "Gupta"); Console.WriteLine($"Person: {person.FirstName} {person.LastName}"); } }
In this example, we will demonstrate value equality in records:
csharppublic record Person(string FirstName, string LastName, string[] PhoneNumbers); public static class Program { public static void Main() { var phoneNumbers = new string[2]; Person person1 = new("Aman", "Gupta", phoneNumbers); Person person2 = new("Aman", "Gupta", phoneNumbers); Console.WriteLine(person1 == person2); // output: True person1.PhoneNumbers[0] = "9144455678"; Console.WriteLine(person1 == person2); // output: True Console.WriteLine(ReferenceEquals(person1, person2)); // output: False } }
The following example demonstrates the use of a with expression to copy an immutable object and change one of the properties:
csharppublic record Person(string FirstName, string LastName) { public required string[] PhoneNumbers { get; init; } } public class Program { public static void Main() { Person person1 = new("Aman", "Gupta") { PhoneNumbers = new string[1] }; Console.WriteLine(person1); // output: Person { FirstName = Aman, LastName = Gupta, PhoneNumbers = System.String[] } Person person2 = person1 with { FirstName = "John" }; Console.WriteLine(person2); // output: Person { FirstName = John, LastName = Gupta, PhoneNumbers = System.String[] } Console.WriteLine(person1 == person2); // output: False person2 = person1 with { PhoneNumbers = new string[1] }; Console.WriteLine(person2); // output: Person { FirstName = Aman, LastName = gupta, PhoneNumbers = System.String[] } Console.WriteLine(person1 == person2); // output: False person2 = person1 with { }; Console.WriteLine(person1 == person2); // output: True } }
In summary, C# records are a powerful addition to the language, providing a convenient way to represent immutable data structures. By understanding their special characteristics and benefits, we can effectively use them in our C# applications to write clean, maintainable, and efficient code.
Copyrights © 2024 letsupdateskills All rights reserved