Introduced in C# 9.0record
Keywords used to define record types (Record Types). Record type is a lightweight data carrier that focuses on representing data. It provides built-in functions such as equality comparison, generating attributes and methods, making writing data classes more concise and efficient.
Basic concepts
- Immutability: Record types are immutable by default, which means that their properties are read-only and cannot be modified.
- Equality: Record types are compared equally based on their values, rather than reference comparisons.
-
Automation Members: Record types can automatically generate constructors, attributes,
ToString
、Equals
andGetHashCode
method.
Declare record type
The declaration of record types is very concise, and the basic syntax is as follows:
public record Person(string FirstName, string LastName, int Age);
This generates a containingFirstName
、LastName
andAge
The record type of the attribute and automatically provide the constructor,ToString
、Equals
andGetHashCode
method.
Immutability and variability
Immutable record: By default, the record type is immutable and the properties are read-only. For example:
public record Person(string FirstName, string LastName, int Age);
Variable record: can be added after the record typewith
Keywords to create variable records that allow modification of properties. For example:
public record Person(string FirstName, string LastName, int Age) with;
Constructor and initialization
The record type automatically generates a constructor, which can be initialized directly through attributes:
var person = new Person("John", "Doe", 30);
Equality comparison
Record types are compared equally based on values:
var person1 = new Person("John", "Doe", 30); var person2 = new Person("John", "Doe", 30); (person1 == person2); // Output: True
Custom methods and members
Custom methods and members can be added to the record type:
public record Person(string FirstName, string LastName, int Age) { public string FullName => $"{FirstName} {LastName}"; }
Property Accessories
By default, the properties of the record type are read-only. If you need to customize the property behavior, you can explicitly define the properties:
public record Person { public string FirstName { get; init; } public string LastName { get; init; } public int Age { get; init; } }
useinit
The accessor can set values when initializing an object, but cannot be modified afterwards.
Inheritance and Derivation
Record types can inherit other record types, and derived record types will inherit properties and methods of the base record type.
public record Person(string FirstName, string LastName, int Age); public record Employee(string FirstName, string LastName, int Age, string EmployeeId) : Person(FirstName, LastName, Age);
Use scenarios
- Data Transfer Object (DTO): Record types are very suitable for representing data transfer objects because they focus on the representation and transmission of data.
- Immutable data structure: Record types provide convenient immutability support when immutable data is required.
- Configuration Objects: Record types can be used to represent the configuration options of the application because they are easy to create and compare.
Sample code
public record Person(string FirstName, string LastName, int Age) { public string FullName => $"{FirstName} {LastName}"; } class Program { static void Main() { var person1 = new Person("John", "Doe", 30); var person2 = new Person("John", "Doe", 30); var person3 = new Person("Jane", "Doe", 25); (person1 == person2); // True (person1 == person3); // False (); // John Doe } }
This is all about this article about C# Record keywords. For more related C# Record keyword content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!