|
1 | 1 | --- |
2 | 2 | title: What's new in C# 14 |
3 | 3 | description: Get an overview of the new features in C# 14. C# 14 ships with .NET 10. |
4 | | -ms.date: 02/19/2025 |
| 4 | +ms.date: 04/04/2025 |
5 | 5 | ms.topic: whats-new |
6 | 6 | --- |
7 | 7 | # What's new in C# 14 |
8 | 8 |
|
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): |
10 | 10 |
|
| 11 | +- [Null-conditional assignment](#null-conditional-assignment) |
11 | 12 | - [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof) |
12 | 13 | - [More implicit conversions for `Span<T>` and `ReadOnlySpan<T>`](#implicit-span-conversions) |
13 | 14 | - [Modifiers on simple lambda parameters](#simple-lambda-parameters-with-modifiers) |
@@ -99,6 +100,31 @@ Only the implementing declaration of a partial constructor can include a constru |
99 | 100 |
|
100 | 101 | The implementing declaration of a partial event must include `add` and `remove` accessors. The defining declaration declares a field-like event. |
101 | 102 |
|
| 103 | +## Null conditional assignment |
| 104 | + |
| 105 | +The null conditional member access operators, `?.` and ``?[]`, can now be used on the left hand side of an assignment or compound assignment. |
| 106 | + |
| 107 | +Before C# 14, you needed to null-check a variable before assigning to a property: |
| 108 | + |
| 109 | +```csharp |
| 110 | +if (customer is not null) |
| 111 | +{ |
| 112 | + customer.Order = GetCurrentOrder(); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +You can simplify the preceding code using the `?.` operator: |
| 117 | + |
| 118 | +```csharp |
| 119 | +customer?.Order = GetCurrentOrder(); |
| 120 | +``` |
| 121 | + |
| 122 | +The right side of the `=` operator is evaluated only when the left side is not null. If `customer` is null, the code won't call `GetCurrentOrder`. |
| 123 | + |
| 124 | +You In addition to assignment, you can use null conditional member access operators with compound assignment operators (`+=`, `-=` and others). However, increment and decrement, `++` and `--` aren't allowed. |
| 125 | + |
| 126 | +You can learn more in the language reference article on the [conditional member access](../language-reference/operators/member-access-operators.md#null-conditional-operators--and-) and the feature specification for [null conditional assignment](~/csharplang/proposals/null-conditional-assignment.md) |
| 127 | + |
102 | 128 | ## See also |
103 | 129 |
|
104 | 130 | - [What's new in .NET 10](../../core/whats-new/dotnet-10/overview.md) |
0 commit comments