You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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]>
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/builtin-types/record.md
+16-14Lines changed: 16 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Records - C# reference"
3
3
description: Learn about the record type in C#
4
-
ms.date: 12/16/2021
4
+
ms.date: 02/25/2022
5
5
f1_keywords:
6
6
- "record_CSharpKeyword"
7
7
helpviewer_keywords:
@@ -10,15 +10,27 @@ helpviewer_keywords:
10
10
---
11
11
# Records (C# reference)
12
12
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:
While records can be mutable, they're primarily intended for supporting immutable data models. The record type offers the following features:
23
35
24
36
*[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
28
40
*[Built-in formatting for display](#built-in-formatting-for-display)
29
41
*[Support for inheritance hierarchies](#inheritance)
30
42
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:
The preceding examples show some distinctions between records that are reference types and records that are value types:
42
44
43
45
- 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.
44
46
- Positional properties are *immutable* in a `record class` and a `readonly record struct`. They're *mutable* in a `record struct`.
45
47
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.
0 commit comments