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
"_csharplang/proposals/field-keyword.md": "This proposal introduces a new keyword, `field`, that accesses the compiler generated backing field in a property accessor.",
813
815
"_csharplang/proposals/unbound-generic-types-in-nameof.md": "This proposal introduces the ability to use unbound generic types such as `List<>` in `nameof` expressions. The type argument isn't required.",
814
816
"_csharplang/proposals/first-class-span-types.md": "This proposal provides several implicit conversions to `Span<T>` and `ReadOnlySpan<T>` that enable library authors to have fewer overloads and developers to write code that resolves to faster Span based APIs",
815
-
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "This proposal provides allows lambda parmaeters to be declared with modifiers without requiring their type names. You can add modifiers like `ref` and `out` to lambda parameters without specifying their type.",
816
-
"_csharplang/proposals/partial-events-and-constructors.md": "This proposal provides allows partial events and constructors to be declared in partial classes. This allows the event and constructor to be split across class declarations.",
817
+
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "This proposal allows lambda parameters to be declared with modifiers without requiring their type names. You can add modifiers like `ref` and `out` to lambda parameters without specifying their type.",
818
+
"_csharplang/proposals/partial-events-and-constructors.md": "This proposal allows partial events and constructors to be declared in partial classes. The event and constructor can be split across class declarations.",
819
+
"_csharplang/proposals/null-conditional-assignment.md": "This proposal allows the null conditional operator to be used for the destination of assignment expressions. This allows you to assign a value to a property or field only if the left side is not null.",
817
820
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11",
818
821
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
819
822
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",
The [arithmetic](arithmetic-operators.md#compound-assignment), [Booleanlogical](boolean-logical-operators.md#compound-assignment), and [bitwiselogicalandshift](bitwise-and-shift-operators.md#compound-assignment) operatorsallsupportcompountassignment.
70
+
The [arithmetic](arithmetic-operators.md#compound-assignment), [Booleanlogical](boolean-logical-operators.md#compound-assignment), and [bitwiselogicalandshift](bitwise-and-shift-operators.md#compound-assignment) operatorsallsupportcompoundassignment.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/member-access-operators.md
+20-6
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Member access and null-conditional operators and expressions:"
3
3
description: "C# operators that you use to access type members or null-conditionally access type members. These operators include the dot operator - `.`, indexers - `[`, `]`, `^` and `..`, and invocation - `(`, `)`."
4
-
ms.date: 07/31/2024
4
+
ms.date: 04/04/2025
5
5
author: pkulikov
6
6
f1_keywords:
7
7
- "._CSharpKeyword"
@@ -35,7 +35,7 @@ helpviewer_keywords:
35
35
---
36
36
# Member access operators and expressions - the dot, indexer, and invocation operators.
37
37
38
-
You use several operators and expressions to access a type member. These operators include member access (`.`), array element or indexer access (`[]`), index-from-end (`^`), range (`..`), null-conditional operators (`?.` and `?[]`), and method invocation (`()`). These include the *null-conditional* member access (`?.`), and indexer access (`?[]`) operators.
38
+
You use several operators and expressions to access a type member. Member access operators include member access (`.`), array element, or indexer access (`[]`), index-from-end (`^`), range (`..`), null-conditional operators (`?.` and `?[]`), and method invocation (`()`). These include the *null-conditional* member access (`?.`), and indexer access (`?[]`) operators.
39
39
40
40
-[`.` (member access)](#member-access-expression-): to access a member of a namespace or a type
41
41
-[`[]` (array element or indexer access)](#indexer-operator-): to access an array element or a type indexer
@@ -138,7 +138,7 @@ The following examples demonstrate the usage of the `?.` and `?[]` operators:
The first of the preceding two examples also uses the [null-coalescing operator `??`](null-coalescing-operator.md) to specify an alternative expression to evaluate in case the result of a null-conditional operation is `null`.
141
+
The first preceding example also uses the [null-coalescing operator `??`](null-coalescing-operator.md) to specify an alternative expression to evaluate in case the result of a null-conditional operation is `null`.
142
142
143
143
If `a.x` or `a[x]` is of a non-nullable value type `T`, `a?.x` or `a?[x]` is of the corresponding [nullable value type](../builtin-types/nullable-value-types.md)`T?`. If you need an expression of type `T`, apply the null-coalescing operator `??` to a null-conditional expression, as the following example shows:
144
144
@@ -147,9 +147,23 @@ If `a.x` or `a[x]` is of a non-nullable value type `T`, `a?.x` or `a?[x]` is of
147
147
In the preceding example, if you don't use the `??` operator, `numbers?.Length < 2` evaluates to `false` when `numbers` is `null`.
148
148
149
149
> [!NOTE]
150
-
> The `?.` operator evaluates its left-hand operand no more than once, guaranteeing that it cannot be changed to `null` after being verified as non-null.
150
+
> The `?.` operator evaluates its left-hand operand no more than once, guaranteeing that it can't be changed to `null` after being verified as non-null.
151
151
152
-
The null-conditional member access operator `?.` is also known as the Elvis operator.
152
+
Beginning in C# 14, assignment is permissible with a null conditional access expression (`?.` and `?[]`) on reference types. For example, see the following method:
The preceding example shows assignment to a property and an indexed element on a reference type that might be null. An important behavior for this assignment is that the expression on the right-hand side of the `=` is evaluated only when the left-hand side is known to be non-null. For example, in the following code, the function `GenerateNextIndex` is called only when the `values` array isn't null. If the `values` array is null, `GenerateNextIndex` isn't called:
In addition to assignment, any form of [compound assignment](./assignment-operator.md#compound-assignment), such as `+=` or `-=`, are allowed. However, increment (`++`) and decrement (`--`) aren't allowed.
165
+
166
+
This enhancement doesn't classify a null conditional expression as a variable. It can't be `ref` assigned, nor can it be assigned to a `ref` variable or passed to a method as a `ref` or `out` argument.
153
167
154
168
### Thread-safe delegate invocation
155
169
@@ -175,7 +189,7 @@ The preceding example is a thread-safe way to ensure that only a non-null `handl
175
189
176
190
Use parentheses, `()`, to call a [method](../../programming-guide/classes-and-structs/methods.md) or invoke a [delegate](../../programming-guide/delegates/index.md).
177
191
178
-
The following example demonstrates how to call a method, with or without arguments, and invoke a delegate:
192
+
The following code demonstrates how to call a method, with or without arguments, and invoke a delegate:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/null-coalescing-operator.md
+6-7
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "?? and ??= operators - null-coalescing operators"
3
3
description: "The `??` and `??=` operators are the C# null-coalescing operators. They return the value of the left-hand operand if it isn't null. Otherwise, they return the value of the right-hand operand"
4
-
ms.date: 11/28/2022
4
+
ms.date: 04/04/2025
5
5
f1_keywords:
6
6
- "??_CSharpKeyword"
7
7
- "??=_CSharpKeyword"
@@ -10,7 +10,6 @@ helpviewer_keywords:
10
10
- "?? operator [C#]"
11
11
- "null-coalescing assignment [C#]"
12
12
- "??= operator [C#]"
13
-
ms.assetid: 088b1f0d-c1af-4fe1-b4b8-196fd5ea9132
14
13
---
15
14
# ?? and ??= operators - the null-coalescing operators
The null-coalescing operator `??` returns the value of its left-hand operand if it isn't `null`; otherwise, it evaluates the right-hand operand and returns its result. The `??` operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. The null-coalescing assignment operator `??=` assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to `null`. The `??=` operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
The left-hand operand of the `??=` operator must be a variable, a [property](../../programming-guide/classes-and-structs/properties.md), or an [indexer](../../programming-guide/indexers/index.md) element.
26
25
27
26
The type of the left-hand operand of the `??` and `??=` operators can't be a non-nullable value type. In particular, you can use the null-coalescing operators with unconstrained type parameters:
28
27
29
-
[!code-csharp[unconstrained type parameter](snippets/shared/NullCoalescingOperator.cs#UnconstrainedType)]
0 commit comments