-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Consolidated the C# nullable value types articles #15676
Consolidated the C# nullable value types articles #15676
Conversation
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.
This is great work @pkulikov
I had one question and a couple suggestions. Let me know your thoughts.
@@ -22,7 +22,7 @@ Available in C# 8.0 and later, the null-coalescing assignment operator `??=` ass | |||
|
|||
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. | |||
|
|||
In C# 7.3 and earlier, the type of the left-hand operand of the `??` operator must be either a reference type or a [nullable value type](../../programming-guide/nullable-types/index.md). Beginning with C# 8.0, that requirement is replaced with the following: the type of the left-hand operand of the `??` and `??=` operators cannot be a non-nullable value type. In particular, beginning with C# 8.0, you can use the null-coalescing operators with unconstrained type parameters: | |||
In C# 7.3 and earlier, the type of the left-hand operand of the `??` operator must be either a [reference type](../keywords/reference-types.md) or a [nullable value type](../builtin-types/nullable-value-types.md). Beginning with C# 8.0, that requirement is replaced with the following: the type of the left-hand operand of the `??` and `??=` operators cannot be a non-nullable value type. In particular, beginning with C# 8.0, you can use the null-coalescing operators with unconstrained type parameters: |
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.
nit: I'd add the quotes to make it clear where the text from the spec ends:
In C# 7.3 and earlier, the type of the left-hand operand of the `??` operator must be either a [reference type](../keywords/reference-types.md) or a [nullable value type](../builtin-types/nullable-value-types.md). Beginning with C# 8.0, that requirement is replaced with the following: the type of the left-hand operand of the `??` and `??=` operators cannot be a non-nullable value type. In particular, beginning with C# 8.0, you can use the null-coalescing operators with unconstrained type parameters: | |
In C# 7.3 and earlier, the type of the left-hand operand of the `??` operator must be either a [reference type](../keywords/reference-types.md) or a [nullable value type](../builtin-types/nullable-value-types.md). Beginning with C# 8.0, that requirement is replaced with the following: "the type of the left-hand operand of the `??` and `??=` operators cannot be a non-nullable value type." In particular, beginning with C# 8.0, you can use the null-coalescing operators with unconstrained type parameters: |
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.
@BillWagner would it be fine to quote not direct spec text? The updated spec text (check the end of the linked section) reads:
If A exists and is a non-nullable value type, a compile-time error occurs.
where A - is the type of the left-hand operand.
|
||
[!code-csharp-interactive[?? operator](~/samples/csharp/language-reference/builtin-types/NullableValueTypes.cs#NullCoalescing)] | ||
|
||
Use the <xref:System.Nullable%601.GetValueOrDefault?displayProperty=nameWithType> method if the value to be used when a value of a nullable value type is `null` should be the [default](../keywords/default-values-table.md) value of the underlying value type. |
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.
This sentence read very awkward for me. How about this version:
Use the <xref:System.Nullable%601.GetValueOrDefault?displayProperty=nameWithType> method if the value to be used when a value of a nullable value type is `null` should be the [default](../keywords/default-values-table.md) value of the underlying value type. | |
Often, you want to use the [default](../keywords/default-values-table.md) value of the underlying value type in place | |
of the `null` of a nullable value type. The <xref:System.Nullable%601.GetValueOrDefault?displayProperty=nameWithType> method accomplishes this. |
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.
@BillWagner I like your wording more, thanks. I've edited it a bit and also tried to apply it to the paragraph above.
|
||
[!code-csharp-interactive[boxing and unboxing](~/samples/csharp/language-reference/builtin-types/NullableValueTypes.cs#Boxing)] | ||
|
||
## How to identify a nullable value type |
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.
Should this section include an "Important" note that these operations do not apply for nullable reference types. ( know that you've been very careful to include "value" in "nullable value type" throughout the article. That makes me think it would be redundant. On the other hand, the syntax would seem to imply that there are similarities between int?
and string?
. That's a bad assumption.
What are your thoughts?
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.
@BillWagner I've added the note to the end of the section. Though being redundant, I think it doesn't hurt.
Also, after this article is published, should the issue #9662 be "redirected" to the new article?
@BillWagner I've addressed your feedback with one item waiting for your response. |
This looks great @pkulikov I'll |
Fixes #14726
Supported by dotnet/samples#1694
Contributes to #13421