Skip to content

Commit 9fbc646

Browse files
author
Ron Petrusha
authored
Addressed language differences for null conditional operator (#11425)
* Addressed language differences * Corrected typo
1 parent b1138b8 commit 9fbc646

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

docs/csharp/language-reference/operators/null-conditional-operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ helpviewer_keywords:
88
- "?[] operator [C#]"
99
ms.assetid: 9c7b2c8f-a785-44ca-836c-407bfb6d27f5
1010
---
11-
# ?. and ?[] null-conditional operators (C# and Visual Basic)
11+
# ?. and ?[] null-conditional operators (C# Reference)
1212

1313
Tests the value of the left-hand operand for null before performing a member access (`?.`) or index (`?[]`) operation; returns `null` if the left-hand operand evaluates to `null`.
1414

docs/visual-basic/language-reference/operators/null-conditional-operators.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ helpviewer_keywords:
99
---
1010
# ?. and ?() null-conditional operators (Visual Basic)
1111

12-
Tests the value of the left-hand operand for null (`Nothing`) before performing a member access (`?.`) or index (`?()`) operation; returns `Nothing` if the left-hand operand evaluates to `Nothing`. Note that, in the expressions that would ordinarily return value types, the null-conditional operator returns a <xref:System.Nullable%601>.
12+
Tests the value of the left-hand operand for null (`Nothing`) before performing a member access (`?.`) or index (`?()`) operation; returns `Nothing` if the left-hand operand evaluates to `Nothing`. Note that in expressions that ordinarily return value types, the null-conditional operator returns a <xref:System.Nullable%601>.
1313

1414
These operators help you write less code to handle null checks, especially when descending into data structures. For example:
1515

1616
```vb
17-
Dim length As Integer? = customers?.Length ' Nothing if customers is Nothing
18-
Dim first As Customer = customers?(0) ' Nothing if customers is Nothing
19-
Dim count As Integer? = customers?(0)?.Orders?.Count() ' Nothing if customers, the first customer, or Orders is Nothing
17+
' Nothing if customers is Nothing
18+
Dim length As Integer? = customers?.Length
19+
20+
' Nothing if customers is Nothing
21+
Dim first As Customer = customers?(0)
22+
23+
' Nothing if customers, the first customer, or Orders is Nothing
24+
Dim count As Integer? = customers?(0)?.Orders?.Count()
2025
```
2126

2227
For comparison, the alternative code for the first of these expressions without a null-conditional operator is:
@@ -28,27 +33,58 @@ If customers IsNot Nothing Then
2833
End If
2934
```
3035

31-
The null-conditional operators are short-circuiting. If one operation in a chain of conditional member access and index operations returns Nothing, the rest of the chain’s execution stops. In the following example, C(E) isn't evaluated if `A`, `B`, or `C` evaluates to Nothing.
36+
The null-conditional operators are short-circuiting. If one operation in a chain of conditional member access and index operations returns `Nothing`, the rest of the chain’s execution stops. In the following example, `C(E)` isn't evaluated if `A`, `B`, or `C` evaluates to `Nothing`.
3237

3338
```vb
3439
A?.B?.C?(E);
3540
```
3641

37-
Another use for null-conditional member access is to invoke delegates in a thread-safe way with much less code. The old way requires code like the following:
42+
Another use for null-conditional member access is to invoke delegates in a thread-safe way with much less code. The following example defines two types, a `NewsBroadcaster` and a `NewsReceiver`. News items are sent to the receiver by the `NewsBroadcaster.SendNews` delegate.
43+
44+
```vb
45+
Public Module NewsBroadcaster
46+
Dim SendNews As Action(Of String)
47+
48+
Public Sub Main()
49+
Dim rec As New NewsReceiver()
50+
Dim rec2 As New NewsReceiver()
51+
SendNews?.Invoke("Just in: A newsworthy item...")
52+
End Sub
53+
54+
Public Sub Register(client As Action(Of String))
55+
SendNews = SendNews.Combine({SendNews, client})
56+
End Sub
57+
End Module
58+
59+
Public Class NewsReceiver
60+
Public Sub New()
61+
NewsBroadcaster.Register(AddressOf Me.DisplayNews)
62+
End Sub
63+
64+
Public Sub DisplayNews(newsItem As String)
65+
Console.WriteLine(newsItem)
66+
End Sub
67+
End Class
68+
```
69+
70+
If there are no elements in the `SendNews` invocation list, the `SendNews` delegate throws a <xref:System.NullReferenceException>. Before
71+
null conditional operators, code like the following ensured that the delegate invocation list was not `Nothing`:
3872

3973
```vb
40-
Dim handler = AddressOf(Me.PropertyChanged)
41-
If handler IsNot Nothing
42-
Call handler()
74+
SendNews = SendNews.Combine({SendNews, client})
75+
If SendNews IsNot Nothing Then
76+
SendNews("Just in...")
77+
End If
4378
```
4479

4580
The new way is much simpler:
4681

4782
```vb
48-
PropertyChanged?.Invoke()
83+
SendNews = SendNews.Combine({SendNews, client})
84+
SendNews?.Invoke("Just in...")
4985
```
5086

51-
The new way is thread-safe because the compiler generates code to evaluate `PropertyChanged` one time only, keeping the result in a temporary variable. You need to explicitly call the `Invoke` method because there is no null-conditional delegate invocation syntax `PropertyChanged?(e)`.
87+
The new way is thread-safe because the compiler generates code to evaluate `SendNews` one time only, keeping the result in a temporary variable. You need to explicitly call the `Invoke` method because there is no null-conditional delegate invocation syntax `SendNews?(String)`.
5288

5389
## See also
5490

0 commit comments

Comments
 (0)