Skip to content

Commit f78bd66

Browse files
Add clarifications on records (#28405)
* Add clarifications on records Fixes #27664 Rework the introduction to clearly state records are a C# 9 feature, and record structs are C# 10. The later sections in the article combine the discussion to emphasize the similarities between records and record structs. * Apply suggestions from code review Co-authored-by: David Pine <[email protected]> Co-authored-by: David Pine <[email protected]>
1 parent 9fb4324 commit f78bd66

File tree

1 file changed

+16
-14
lines changed
  • docs/csharp/language-reference/builtin-types

1 file changed

+16
-14
lines changed

docs/csharp/language-reference/builtin-types/record.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Records - C# reference"
33
description: Learn about the record type in C#
4-
ms.date: 12/16/2021
4+
ms.date: 02/25/2022
55
f1_keywords:
66
- "record_CSharpKeyword"
77
helpviewer_keywords:
@@ -10,15 +10,27 @@ helpviewer_keywords:
1010
---
1111
# Records (C# reference)
1212

13-
Beginning with C# 9, you use the `record` keyword to define a [reference type](reference-types.md) that provides built-in functionality for encapsulating data. You can create record types with immutable properties by using positional parameters or standard property syntax:
13+
Beginning with C# 9, you use the `record` keyword to define a [reference type](reference-types.md) that provides built-in functionality for encapsulating data. C# 10 allows the `record class` syntax as a synonym to clarify a reference type, and `record struct` to define a [value type](value-types.md) with similar functionality. You can create record types with immutable properties by using positional parameters or standard property syntax.
14+
15+
The following two examples demonstrate `record` (or `record class`) reference types:
1416

1517
:::code language="csharp" source="snippets/shared/RecordType.cs" id="PositionalRecord":::
1618
:::code language="csharp" source="snippets/shared/RecordType.cs" id="ImmutableRecord":::
1719

18-
You can also create record types with mutable properties and fields:
20+
The following two examples demonstrate `record struct` value types:
21+
22+
:::code language="csharp" source="snippets/shared/RecordType.cs" id="PositionalRecordStruct":::
23+
:::code language="csharp" source="snippets/shared/RecordType.cs" id="ImmutableRecordStruct":::
24+
25+
You can also create records with mutable properties and fields:
1926

2027
:::code language="csharp" source="snippets/shared/RecordType.cs" id="MutableRecord":::
2128

29+
Record structs can be mutable as well, both positional record structs and record structs with no positional parameters:
30+
31+
:::code language="csharp" source="snippets/shared/RecordType.cs" id="MutablePositionalRecordStruct":::
32+
:::code language="csharp" source="snippets/shared/RecordType.cs" id="MutableRecordStruct":::
33+
2234
While records can be mutable, they're primarily intended for supporting immutable data models. The record type offers the following features:
2335

2436
* [Concise syntax for creating a reference type with immutable properties](#positional-syntax-for-property-definition)
@@ -28,22 +40,12 @@ While records can be mutable, they're primarily intended for supporting immutabl
2840
* [Built-in formatting for display](#built-in-formatting-for-display)
2941
* [Support for inheritance hierarchies](#inheritance)
3042

31-
You can also use [structure types](struct.md) to design data-centric types that provide value equality and little or no behavior. In C# 10 and later, you can define `record struct` types using either positional parameters, or standard property syntax:
32-
33-
:::code language="csharp" source="snippets/shared/RecordType.cs" id="PositionalRecordStruct":::
34-
:::code language="csharp" source="snippets/shared/RecordType.cs" id="ImmutableRecordStruct":::
35-
36-
Record structs can be mutable as well, both positional record structs and record structs with no positional parameters:
37-
38-
:::code language="csharp" source="snippets/shared/RecordType.cs" id="MutablePositionalRecordStruct":::
39-
:::code language="csharp" source="snippets/shared/RecordType.cs" id="MutableRecordStruct":::
40-
4143
The preceding examples show some distinctions between records that are reference types and records that are value types:
4244

4345
- A `record` or a `record class` declares a reference type. The `class` keyword is optional, but can add clarity for readers. A `record struct` declares a value type.
4446
- Positional properties are *immutable* in a `record class` and a `readonly record struct`. They're *mutable* in a `record struct`.
4547

46-
The remainder of this article discusses both `record class` and `record struct` types. The differences are detailed in each section. You should decide between a `record class` and a `record struct` similar to deciding between a `class` and a `struct`. The term *record* is used to describe behavior that applies to all record types. Either `record struct` or `record class` is used to describe behavior that applies to only struct or class types, respectively.
48+
The remainder of this article discusses both `record class` and `record struct` types. The differences are detailed in each section. You should decide between a `record class` and a `record struct` similar to deciding between a `class` and a `struct`. The term *record* is used to describe behavior that applies to all record types. Either `record struct` or `record class` is used to describe behavior that applies to only struct or class types, respectively. The `record` type were introduced in C# 9; `record struct` types were introduced in C# 10.
4749

4850
## Positional syntax for property definition
4951

0 commit comments

Comments
 (0)