Skip to content

How to Use Record Structs in C#

How to Use Record Structs in C#
Become a developer with our complete learning paths
Become a developer with our complete learning paths

How to Use Record Structs in C#

Introduction to Record Structs in C#

Record structs, introduced in C# 9.0, are a type of value type that combines features of structs and records. They are designed to provide an efficient way to represent simple data aggregates with value-based equality semantics.

Key Features and Usage

  1. Immutable by Default:

    • Record structs are immutable, meaning their state cannot be altered after they are created.
    • This immutability is enforced by the init accessor in property declarations, which allows property values to be set only during object initialization.
  2. Value-Based Equality:

    • Record structs automatically implement IEquatable<T>, enabling value-based equality checks instead of reference-based (default for objects).
    • This feature is particularly useful for structs as they are often used to represent simple data types where value equality is more relevant than reference identity.
  3. Built-In Support for Interfaces:

    • Implementations for IStructuralEquatable and IStructuralComparable are provided out of the box.
    • These interfaces support advanced comparison and sorting operations based on the values of the struct’s properties.
  4. Nondestructive Mutation with the with Expression:

    • The with keyword creates a new record struct instance by copying existing values and allowing modifications.
    • This is useful for creating variations of immutable structures without altering the original instance.

Code Example with with Expression

Additional Considerations

  1. Deconstructor Support:

    • Record structs can include a deconstructor, allowing properties to be easily decomposed into variables.
  2. Record Structs vs Record Classes:

    • Unlike record classes (which are reference types), record structs are value types and are stored on the stack, leading to potential performance benefits in certain scenarios.
  3. Suitable Use Cases:

    • They are ideal for small, immutable data structures, especially where value equality and efficient copying are important, such as in functional programming paradigms.
  4. Limitations:

    • Being value types, record structs can lead to increased memory usage if used improperly, especially with large data sets.

Conclusion

Record structs in C# provide a robust and efficient way to handle simple data structures with value-based equality and immutability. They streamline certain programming tasks, particularly where data integrity and consistent behavior are paramount. Understanding when and how to use them effectively can be a valuable addition to a C# developer’s toolkit.

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Become a senior C# developer and build software like an A-Player
Join the C# Progress Academy and master C# .NET, ASP.NET, Design Patterns, Asynchronous Development, Clean Code, and much more.
Become a senior C# developer and build software like an A-Player
Join the C# Progress Academy and master C# .NET, ASP.NET, Design Patterns, Asynchronous Development, Clean Code, and much more.