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
Copy file name to clipboardExpand all lines: docs/csharp/whats-new/csharp-14.md
+37-4Lines changed: 37 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,9 @@ ms.topic: whats-new
9
9
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):
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)
14
15
15
16
C# 14 is supported on **.NET 10**. For more information, see [C# language versioning](../language-reference/configure-language-version.md).
16
17
@@ -24,14 +25,46 @@ You can find any breaking changes introduced in C# 14 in our article on [breakin
24
25
25
26
## The `field` keyword
26
27
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
+
privatestring_msg;
34
+
publicstringMessage
35
+
{
36
+
get=>_msg;
37
+
set
38
+
{
39
+
if (valueisnull) thrownewNullArgumentException(nameof(value));
You can declare a body for one or both accessors for a field backed property.
28
59
29
60
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.
If you try this feature and have feedback, comment on the [feature issue](https://github.com/dotnet/csharplang/issues/140) in the `csharplang` repository.
34
65
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
+
35
68
## Implicit span conversions
36
69
37
70
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
42
75
43
76
## Unbound generic types and nameof
44
77
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.
0 commit comments