diff --git a/docs/csharp/language-reference/attributes/general.md b/docs/csharp/language-reference/attributes/general.md index 4e99b4c3cd177..30deaf8f1be05 100644 --- a/docs/csharp/language-reference/attributes/general.md +++ b/docs/csharp/language-reference/attributes/general.md @@ -45,7 +45,7 @@ The string provided as the first argument to the attribute constructor will be d In C# 10, you can use constant string interpolation and the `nameof` operator to ensure the names match: -:::code language="csharp" source="snippets/ObsoleteExample.cs" ID="Snippet2" ::: +:::code language="csharp" source="snippets/ObsoleteExample.cs" id="Snippet2" ::: ## `AttributeUsage` attribute diff --git a/docs/csharp/language-reference/builtin-types/record.md b/docs/csharp/language-reference/builtin-types/record.md index bba9566da2bb2..d468a7b8781ca 100644 --- a/docs/csharp/language-reference/builtin-types/record.md +++ b/docs/csharp/language-reference/builtin-types/record.md @@ -1,7 +1,7 @@ --- title: "Records - C# reference" description: Learn about the record type in C# -ms.date: 02/25/2021 +ms.date: 07/01/2021 f1_keywords: - "record_CSharpKeyword" helpviewer_keywords: @@ -136,7 +136,7 @@ The `ToString` override creates a object with t :::code language="csharp" source="snippets/shared/RecordType.cs" id="ToStringOverrideDefault"::: -You can provide your own implementation of `PrintMembers` or the `ToString` override. Examples are provided in the [`PrintMembers` formatting in derived records](#printmembers-formatting-in-derived-records) section later in this article. +You can provide your own implementation of `PrintMembers` or the `ToString` override. Examples are provided in the [`PrintMembers` formatting in derived records](#printmembers-formatting-in-derived-records) section later in this article. In C# 10 and later, your implementation of `ToString` may include the `sealed` modifier, which prevents the compiler from synthesizing a `ToString` implementation for any derived records. Effectively, that means the `ToString` output will not include the runtime type information. (All members and values are displayed, because derived records will still have a PrintMembers method generated.) ## Inheritance @@ -185,6 +185,9 @@ Here is an example of code that replaces the synthesized `PrintMembers` methods, :::code language="csharp" source="snippets/shared/RecordType.cs" id="PrintMembersImplementation"::: +> [!NOTE] +> In C# 10.0 and later, the compiler will synthesize `PrintMembers` when a base record has sealed the `ToString` method. You can also create your own implementation of `PrintMembers`. + ### Deconstructor behavior in derived records The `Deconstruct` method of a derived record returns the values of all positional properties of the compile-time type. If the variable type is a base record, only the base record properties are deconstructed unless the object is cast to the derived type. The following example demonstrates calling a deconstructor on a derived record. diff --git a/docs/csharp/whats-new/tutorials/records.md b/docs/csharp/whats-new/tutorials/records.md index dd58a59807abf..9894f653d4390 100644 --- a/docs/csharp/whats-new/tutorials/records.md +++ b/docs/csharp/whats-new/tutorials/records.md @@ -1,7 +1,7 @@ --- title: Use record types - C# tutorial description: Learn about how to use record types, build hierarchies of records, and when to choose records over classes. -ms.date: 11/12/2020 +ms.date: 07/01/2021 --- # Create record types @@ -125,6 +125,8 @@ You declare a `PrintMembers` method in the `DegreeDays` record that doesn't prin The signature declares a `virtual protected` method to match the compiler's version. Don't worry if you get the accessors wrong; the language enforces the correct signature. If you forget the correct modifiers for any synthesized method, the compiler issues warnings or errors that help you get the right signature. +In C# 10.0 and later, you can declare the `ToString` method as `sealed` in a record type. That prevents derived records from providing a new implementation. Derived records will still contain the `PrintMembers` override. You would do this if you didn't want the `ToString` method to display the runtime type of the record. In the preceding example, you'd lose the information on where the record was measuring heating or cooling degree days. + ## Non-destructive mutation The synthesized members in a positional record don't modify the state of the record. The goal is that you can more easily create immutable records. Look again at the preceding declarations for `HeatingDegreeDays` and `CoolingDegreeDays`. The members added perform computations on the values for the record, but don't mutate state. Positional records make it easier for you to create immutable reference types. diff --git a/docs/csharp/whats-new/tutorials/snippets/record-types/record-types.csproj b/docs/csharp/whats-new/tutorials/snippets/record-types/record-types.csproj index 5d38b2381cbf9..0a4163c9e5e77 100644 --- a/docs/csharp/whats-new/tutorials/snippets/record-types/record-types.csproj +++ b/docs/csharp/whats-new/tutorials/snippets/record-types/record-types.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 record_types