Skip to content

Commit 1ae2f30

Browse files
committed
Add links from Whats New in C#
Also, fix warnings.
1 parent 44afb80 commit 1ae2f30

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

docs/csharp/language-reference/operators/assignment-operator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This operation is called *value assignment*: the value is assigned.
3333

3434
Beginning with C# 14, the left hand side of a value assignment can include a [null conditional member expression](./member-access-operators.md#null-conditional-operators--and-), such as `?.` or `?[]`. If the left hand side is null, the right hand side expression isn't evaluated.
3535

36-
## Reference assignment
36+
## ref assignment
3737

3838
*Ref assignment* `= ref` makes its left-hand operand an alias to the right-hand operand, as the following example demonstrates:
3939

docs/csharp/language-reference/operators/snippets/shared/NullCoalescingOperator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace operators;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace operators;
24

35
public static class NullCoalescingOperator
46
{
@@ -9,11 +11,14 @@ public static void Examples()
911
NullCoalescingAssignment();
1012
}
1113

12-
public record class Person(string FirstName, string LastName);
14+
public record class Human(string FirstName, string LastName)
15+
{
16+
public string FirstName { get; set; } = FirstName;
17+
}
1318
public static void AddMessageAtIndex()
1419
{
1520
List<string> messages = new List<string>(10);
16-
Person person = new Person("First", "Last");
21+
Human person = new Human("First", "Last");
1722
// <NullForgivingAssignment>
1823
person?.FirstName = "Scott";
1924
messages?[5] = "five";

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
22
title: What's new in C# 14
33
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
55
ms.topic: whats-new
66
---
77
# What's new in C# 14
88

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

11+
- [Null-conditional assignment](#null-conditional-assignment)
1112
- [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof)
1213
- [More implicit conversions for `Span<T>` and `ReadOnlySpan<T>`](#implicit-span-conversions)
1314
- [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
99100

100101
The implementing declaration of a partial event must include `add` and `remove` accessors. The defining declaration declares a field-like event.
101102

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+
102128
## See also
103129

104130
- [What's new in .NET 10](../../core/whats-new/dotnet-10/overview.md)

0 commit comments

Comments
 (0)