-
Couldn't load subscription status.
- Fork 6.1k
Fixed VB code and syntax highlighting #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,48 +33,51 @@ Used to test for null before performing a member access (`?.`) or index (`?[`) o | |
| int? length = customers?.Length; // null if customers is null | ||
| Customer first = customers?[0]; // null if customers is null | ||
| int? count = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null | ||
|
|
||
| ``` | ||
|
|
||
| ```vb | ||
| Dim length = customers?.Length ‘’ null if customers is null | ||
| Dim first as Customer = customers?(0); ‘’ null if customers is null | ||
| Dim count as Integer? = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null | ||
|
|
||
| Dim length = customers?.Length ' null if customers is null | ||
| Dim first as Customer = customers?(0) ' null if customers is null | ||
| Dim count as Integer? = customers?(0)?.Orders?.Count() ' null if customers, the first customer, or Orders is null | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ```vb-c# | ||
| ```csharp | ||
| A?.B?.C?[0] ?? E | ||
| A?.B?.C?[0] == E | ||
|
|
||
| ``` | ||
|
|
||
| ```vb | ||
| A?.B?.C?(0) ?? E | ||
| A?.B?.C?(0) == E | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```csharp | ||
| var handler = this.PropertyChanged; | ||
| if (handler != null) | ||
| handler(…) | ||
|
|
||
| handler(…); | ||
| ``` | ||
|
|
||
| ```vb | ||
| Dim handler = AddressOf(Me.PropertyChanged) | ||
| If handler IsNot Nothing | ||
| Call handler(…) | ||
|
|
||
| ``` | ||
|
|
||
| The new way is much simpler: | ||
|
|
||
| ```vb-c# | ||
| ```csharp | ||
| PropertyChanged?.Invoke(e) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you break this out into two separate code blocks (even though they're basically identical)? PropertyChanged?.Invoke(e);PropertyChanged?.Invoke(e) |
||
|
|
||
| ``` | ||
|
|
||
| ```vb | ||
| PropertyChanged?.Invoke(e) | ||
| ``` | ||
|
|
||
| The new way is thread-safe because the compiler generates code to evaluate `PropertyChanged` one time only, keeping the result in temporary variable. | ||
| 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)`. There were too many ambiguous parsing situations to allow it. | ||
|
|
||
|
|
@@ -88,4 +91,4 @@ PropertyChanged?.Invoke(e) | |
| [C# Reference](../../../csharp/language-reference/index.md) | ||
| [C# Programming Guide](../../../csharp/programming-guide/index.md) | ||
| [Visual Basic Language Reference](../../../visual-basic/language-reference/index.md) | ||
| [Visual Basic Programming Guide](../../../visual-basic/programming-guide/index.md) | ||
| [Visual Basic Programming Guide](../../../visual-basic/programming-guide/index.md) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@svick Can you add semicolons at the ends of the lines of C# code?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are not valid C# statements, so semicolons don't make sense here. I think they are just supposed to be two separate expressions.
Do you think it would be clearer if they were expanded to statements? Something like: