In the ever-evolving landscape of programming, the introduction of C# record struct in version 9.0 has brought a significant shift in how we handle value types in the language. This new feature is designed to simplify the process of creating immutable value types, making your code more efficient and easier to understand.
Understanding C# Records
Before delving into C# record struct, let's first understand what records are. In simple terms, a record is a reference type that encapsulates equality comparison, hashing, and formatting. It's a great way to represent a data carrier or a data transfer object (DTO).
Why Use Records?
- Immutability: Records are immutable, meaning once created, their values cannot be changed. This ensures data consistency and makes your code easier to reason about.
- Equality and Hashing: Records automatically provide value-based equality and hashing, eliminating the need for manual implementation.
- Formatting: Records support formatting using the
ToStringmethod, making them easier to print or display.
Introducing C# Record Struct
Now that we understand records, let's explore the C# record struct. A record struct is a value type that inherits from the Record class. It combines the benefits of records (immutability, automatic equality, hashing, and formatting) with the performance and stack allocation of structs.

Benefits of C# Record Struct
- Performance: As a value type, C# record struct is stack-allocated, leading to better performance compared to reference types.
- Immutability: Like records, C# record struct is immutable, ensuring data consistency and simplifying your code.
- Simplified Equality: C# record struct automatically provides value-based equality, eliminating the need for manual implementation.
Creating a C# Record Struct
Creating a C# record struct is straightforward. Here's a simple example:
public record struct Point(int X, int Y)
{
public override string ToString() => $"({X}, {Y})";
}
With Expressions
You can also create C# record struct using expressions, which can simplify your code further:
public record struct Point(int X, int Y) => new ValueTuple<int, int>(X, Y);
Conclusion
The introduction of C# record struct has brought significant improvements to how we handle value types in C#. By providing automatic equality, hashing, and formatting, along with the performance benefits of structs, C# record struct makes your code more efficient, easier to understand, and less prone to errors. Embrace this new feature and elevate your C# coding experience.






















