Skip to content

Commit

Permalink
Specify record struct initialization (dotnet#27595)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillWagner authored Dec 16, 2021
1 parent 390ba1e commit 3fe194c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 33 deletions.
32 changes: 0 additions & 32 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion docs/csharp/whats-new/tutorials/records.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The formula is based on the mean temperature on a given day and a baseline tempe

:::code language="csharp" source="snippets/record-types/InterimSteps.cs" ID="DailyRecord":::

The preceding code defines a *positional record*. The `DailyTemperature` record is a `readonly record struct`, because you don't intend to inherit from it, and it should be immutable. The `HighTemp` and `LowTemp` properties are *init only properties*, meaning they can be set in the constructor or using a property initializer. If you wanted the positional parameters to be read-write, you declare a `record struct` instead of a `readonly record struct`. The `DailyTemperature` type also has a *primary constructor* that has two parameters that match the two properties. You use the primary constructor to initialize a `DailyTemperature` record:
The preceding code defines a *positional record*. The `DailyTemperature` record is a `readonly record struct`, because you don't intend to inherit from it, and it should be immutable. The `HighTemp` and `LowTemp` properties are *init only properties*, meaning they can be set in the constructor or using a property initializer. If you wanted the positional parameters to be read-write, you declare a `record struct` instead of a `readonly record struct`. The `DailyTemperature` type also has a *primary constructor* that has two parameters that match the two properties. You use the primary constructor to initialize a `DailyTemperature` record. The following code creates and initializes several `DailyTemperature` records. The first uses named parameters to clarify the `HighTemp` and `LowTemp`. The remaining initializers use positional parameters to initialize the `HighTemp` and `LowTemp`:

:::code language="csharp" source="snippets/record-types/Program.cs" ID="DeclareData":::

Expand Down

0 comments on commit 3fe194c

Please sign in to comment.