What Is the Need of Record Type
One of the primary benefits of using record types is that they provide a more concise syntax for defining classes that only hold data. This makes the code easier to read and understand, while also reducing the amount of code that needs to be written. Additionally, record types are designed to be immutable by default, which helps to prevent bugs caused by mutable state.
Another advantage of using record types is that they come with built-in support for equality comparisons, hash codes, and string representations.
Introduction to Record Types in C#
Record types in C# provide a concise way to define types for small, immutable data structures. When you create a new record, you can pass values for the properties in the constructor, and then access those values using the property syntax. They are used to simplify the creation and usage of objects that have a well-defined structure and do not need to be modified after creation.
The public keyword makes the record type accessible to other classes in the program. The record keyword defines the type of the object, followed by the name of the record type. The ParameterList defines the properties of the record type. Here is an example that has been done in which I have stored company details that like name, department, and category, below is the code for it:
// Define a record called CompanyDetails with three string properties: Name, Department, and Category
record CompanyDetails(string Name, string Department, string Category);
class Program
{
static void Main(string[] args)
{
// Create a new instance of the CompanyDetails record and pass in the property values
CompanyDetails company = new CompanyDetails("Linux hint", "Content Writing", "Linux");
// Print out the company name, department, and category
Console.WriteLine($"Company Name: {company.Name}");
Console.WriteLine($"Department: {company.Department}");
Console.WriteLine($"Category: {company.Category}");
}
}
In this program, we create a record called CompanyDetails that has three properties: Name, Department, and Category, each of which is a string. We then create a new instance of CompanyDetails called company, and pass in values for each of the properties. The Console.WriteLine statements then output the values of the Name, Department, and Category properties of the company object, here is the output of the code:
You can also define multiple record types in C#, and each record type can have its own unique set of properties. For example, you can define one record type for Employee and other for the company details and here is the code for that:
// Defining a record to store details of a company
record CompanyDetails(string Name, string Department, string Category);
// Defining a record to store details of an employee
record EmployeeDetails(string Name, int EmployeeID, int Age);
class Program
{
static void Main(string[] args)
{
// Creating an instance of the CompanyDetails record and initializing its values
var companyDetails = new CompanyDetails("Linux hint", "Content Writing", "Linux");
// Printing out the values of the CompanyDetails record using string interpolation
Console.WriteLine($"Company Name: {companyDetails.Name}");
Console.WriteLine($"Department: {companyDetails.Department}");
Console.WriteLine($"Category: {companyDetails.Category}");
// Creating an instance of the EmployeeDetails record and initializing its values
var employeeDetails = new EmployeeDetails("Mark", 7834, 25);
// Printing out the values of the EmployeeDetails record using string interpolation
Console.WriteLine($"Employee Name: {employeeDetails.Name}");
Console.WriteLine($"Employee ID: {employeeDetails.EmployeeID}");
Console.WriteLine($"Employee Age: {employeeDetails.Age}");
}
}
First, we define a record type CompanyDetails with three properties: CompanyName, Department, and Category. We then create a new instance of the CompanyDetails record and initialize its properties with the values “Linux hint”, “Content Writing”, and “Linux”.
Next, we define another record type EmployeeDetails with three properties: Name, EmployeeID, and Age. We then create a new instance of the EmployeeDetails record and initialize its properties with the values “Mark”, 7834, and 25. Finally, we use Console.WriteLine statements to output the values of the properties of both the CompanyDetails and EmployeeDetails records, here is the output of the code:
Conclusion
The record types are a new feature that was introduced in C# 9 which provides a simplified syntax for creating classes that store data. They provide several benefits, including a concise syntax, automatic equality comparison, hashing, printing, and easy creation of immutable objects.
However, they also have some limitations, such as the inability to inherit from other classes and limited functionality for complex logic. Overall, record types are a useful tool for C# developers and can improve the readability and maintainability of their code.