Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# Records

The record keyword is used to create a record class or a record struct. Using the record keyword alone creates a record class and for record structs, use record struct. It can be a readonly struct by using readonly record struct.

Reasons to use Records

  1. Provides value equality checks based on the values
  2. Provides a neat implementation of the ToString() method by default
  3. Allows for non-destructive mutation using the with keyword
  4. When you want immutability by default (for positional syntax when using record or record class)

Immutability of Records

Record Declaration Syntax Property Behavior by Default Immutability
record Person(string Name); get; init; Immutable
record class Person(string Name); get; init; Immutable
record struct Point(int X); get; set; Mutable
readonly record struct Point(int X); get; init; Immutable
Standard body syntax { ... } None (Must be explicitly typed) Depends on definition

Positional records and deconstruction

Creating a record using positional syntax allows for deconstruction.

Record inheritance

A record class can only inherit from another record class. It can't inherit from a regular class, nor can a regular class inherit from a record.

About

A program using C# records.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages