Skip to content

Commit 81822b8

Browse files
committed
Respond to feedback.
1 parent ec35a29 commit 81822b8

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

docs/csharp/whats-new/csharp-14.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ ms.topic: whats-new
99
C# 14 includes the following new features. You can try these features using the latest [Visual Studio 2022](https://visualstudio.microsoft.com/vs/preview/) version or the [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet):
1010

1111
- [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof)
12-
13-
C# 14 includes the [`field`](#the-field-keyword) contextual keyword as a preview feature.
12+
- [More implicit conversions for `Span<T>` and `ReadOnlySpan<T>`](#implicit-span-conversions)
13+
- [Modifiers on simple lambda parameters](#simple-lambda-parameters-with-modifiers)
14+
- [`field` backed properties](#the-field-keyword)
1415

1516
C# 14 is supported on **.NET 10**. For more information, see [C# language versioning](../language-reference/configure-language-version.md).
1617

@@ -24,14 +25,46 @@ You can find any breaking changes introduced in C# 14 in our article on [breakin
2425

2526
## The `field` keyword
2627

27-
The [`field`](../language-reference/keywords/field.md) contextual keyword is in C# 13 as a preview feature. The token `field` accesses the compiler synthesized backing field in a property accessor. It enables you to write an accessor body without declaring an explicit backing field in your type declaration. You can declare a body for one or both accessors for a field backed property.
28+
The token `field` enables you to write a property accessor body without declaring an explicit backing field. The token `field` is replaced with a compiler synthesized backing field.
29+
30+
For example, previously, if you wanted to ensure that a `string` property couldn't be set to `null`, you had to declare a backing field and implement both accessors:
31+
32+
```csharp
33+
private string _msg;
34+
public string Message
35+
{
36+
get => _msg;
37+
set
38+
{
39+
if (value is null) throw new NullArgumentException(nameof(value));
40+
_msg = value;
41+
}
42+
}
43+
```
44+
45+
You can now simplify your code to:
46+
47+
```csharp
48+
public string Message
49+
{
50+
get;
51+
set
52+
{
53+
field = (value is not null) ? value : throw new NullArgumentException(nameof(value));
54+
}
55+
}
56+
```
57+
58+
You can declare a body for one or both accessors for a field backed property.
2859

2960
There's a potential breaking change or confusion reading code in types that also include a symbol named `field`. You can use `@field` or `this.field` to disambiguate between the `field` keyword and the identifier, or you can rename the current `field` symbol to provide more distinction.
3061

3162
[!INCLUDE[field-preview](../includes/field-preview.md)]
3263

3364
If you try this feature and have feedback, comment on the [feature issue](https://github.com/dotnet/csharplang/issues/140) in the `csharplang` repository.
3465

66+
The [`field`](../language-reference/keywords/field.md) contextual keyword is in C# 13 as a preview feature. You can try it if you're using .NET 9 and C# 13 to provide [feedback](https://github.com/dotnet/csharplang/issues/140).
67+
3568
## Implicit span conversions
3669

3770
C# 14 introduces first-class support for <xref:System.Span`1?displayProperty=fullName> and <xref:System.ReadOnlySpan`1?displayProperty=fullName> in the language. This support involves new implicit conversions allowing more natural programming with these integral types.
@@ -42,7 +75,7 @@ You can find the list of implicit span conversions in the article on [built-in t
4275

4376
## Unbound generic types and nameof
4477

45-
Beginning with C# 14, the argument to `nameof` can be an unbound generic type. For example, `nameof(List<>)` evaluates to `List`. In earlier versions of C#, only closed generic types, such as `List<int>`, could be used to produce `List`.
78+
Beginning with C# 14, the argument to `nameof` can be an unbound generic type. For example, `nameof(List<>)` evaluates to `List`. In earlier versions of C#, only closed generic types, such as `List<int>`, could be used to return the `List` name.
4679

4780
## Simple lambda parameters with modifiers
4881

0 commit comments

Comments
 (0)