Skip to content

Commit 61a0936

Browse files
author
Ron Petrusha
authored
Merge pull request dotnet#1799 from svick/patch-5
Fixed VB code and syntax highlighting
2 parents 400dfda + 5884787 commit 61a0936

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,51 @@ Used to test for null before performing a member access (`?.`) or index (`?[`) o
3333
int? length = customers?.Length; // null if customers is null
3434
Customer first = customers?[0]; // null if customers is null
3535
int? count = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null
36-
3736
```
3837

3938
```vb
40-
Dim length = customers?.Length ‘’ null if customers is null
41-
Dim first as Customer = customers?(0); ‘’ null if customers is null
42-
Dim count as Integer? = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null
43-
39+
Dim length = customers?.Length ' null if customers is null
40+
Dim first as Customer = customers?(0) ' null if customers is null
41+
Dim count as Integer? = customers?(0)?.Orders?.Count() ' null if customers, the first customer, or Orders is null
4442
```
4543

4644
The last example demonstrates that the null-condition operators are short-circuiting. If one operation in a chain of conditional member access and index operation returns null, then the rest of the chain’s execution stops. Other operations with lower precedence in the expression continue. For example, `E` in the following always executes, and the `??` and `==` operations execute.
4745

48-
```vb-c#
46+
```csharp
4947
A?.B?.C?[0] ?? E
5048
A?.B?.C?[0] == E
51-
49+
```
50+
51+
```vb
52+
A?.B?.C?(0) ?? E
53+
A?.B?.C?(0) == E
5254
```
5355

5456
Another use for the null-condition member access is invoking delegates in a thread-safe way with much less code. The old way requires code like the following:
5557

5658
```csharp
5759
var handler = this.PropertyChanged;
5860
if (handler != null)
59-
handler(…)
60-
61+
handler(…);
6162
```
6263

6364
```vb
6465
Dim handler = AddressOf(Me.PropertyChanged)
6566
If handler IsNot Nothing
6667
Call handler()
67-
6868
```
6969

7070
The new way is much simpler:
7171

72-
```vb-c#
72+
```csharp
7373
PropertyChanged?.Invoke(e)
74-
74+
```
75+
76+
```vb
77+
PropertyChanged?.Invoke(e)
7578
```
7679

77-
The new way is thread-safe because the compiler generates code to evaluate `PropertyChanged` one time only, keeping the result in temporary variable.
80+
The new way is thread-safe because the compiler generates code to evaluate `PropertyChanged` one time only, keeping the result in a temporary variable.
7881

7982
You need to explicitly call the `Invoke` method because there is no null-conditional delegate invocation syntax `PropertyChanged?(e)`. There were too many ambiguous parsing situations to allow it.
8083

@@ -88,4 +91,4 @@ PropertyChanged?.Invoke(e)
8891
[C# Reference](../../../csharp/language-reference/index.md)
8992
[C# Programming Guide](../../../csharp/programming-guide/index.md)
9093
[Visual Basic Language Reference](../../../visual-basic/language-reference/index.md)
91-
[Visual Basic Programming Guide](../../../visual-basic/programming-guide/index.md)
94+
[Visual Basic Programming Guide](../../../visual-basic/programming-guide/index.md)

0 commit comments

Comments
 (0)