-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix incorrect remarks for Nullable<T>.Implicit operator #2711
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -536,11 +536,17 @@ | |||||||||
<format type="text/markdown"><![CDATA[ | ||||||||||
|
||||||||||
## Remarks | ||||||||||
If the `value` parameter is not `null`, the <xref:System.Nullable%601.Value%2A> property of the new <xref:System.Nullable%601> value is initialized to the `value` parameter and the <xref:System.Nullable%601.HasValue%2A> property is initialized to `true`. | ||||||||||
|
||||||||||
If the `value` parameter is `null`, the <xref:System.Nullable%601.Value%2A> property of the new <xref:System.Nullable%601> value is initialized to the default value, which is the value that is all binary zeroes, and the <xref:System.Nullable%601.HasValue%2A> property is initialized to `false`. | ||||||||||
|
||||||||||
The equivalent method for this operator is <xref:System.Nullable%601.%23ctor%28%600%29?displayProperty=nameWithType>]]></format> | ||||||||||
The <xref:System.Nullable%601.Value%2A> property of the new <xref:System.Nullable%601> value is initialized to the `value` parameter and the <xref:System.Nullable%601.HasValue%2A> property is initialized to `true`. | ||||||||||
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. How about this as an alternative:
Suggested change
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. I don't understand how my proposed change is misleading. This documentation topic is not about implicit nullable conversions in general (such as
The operand is of type 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. @MichaelJLiu The reason I say it's "misleading" is that it implies this wouldn't be allowed: int? a = null; Yes, it's true that the above code does not use the implicit conversion operator. But, readers will make the improper assumption. Adding the one sentence removes that assumption, but doesn't try to expand the article to cover the compiler's behavior to do the assignment. I hope that helps. 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. I asked @ericlippert for his opinion, which I share here with his permission:
I believe (1) is addressed by my original PR. This operator creates a For (2), how about the following?
|
||||||||||
|
||||||||||
The equivalent method for this operator is <xref:System.Nullable%601.%23ctor%28%600%29?displayProperty=nameWithType> | ||||||||||
|
||||||||||
> [!NOTE] | ||||||||||
> In C# and Visual Basic, an implicit conversion from `T` to <xref:System.Nullable%601> does not invoke this operator because these languages have special rules for the conversion. This operator is provided for the benefit of languages that do not have such special rules. | ||||||||||
|
||||||||||
> [!NOTE] | ||||||||||
> In C# and Visual Basic, an implicit conversion from a `null` or `Nothing` literal to <xref:System.Nullable%601> produces a <xref:System.Nullable%601> value whose <xref:System.Nullable%601.HasValue%2A> property is initialized to `false`. The conversion occurs at compile time in these languages and does not invoke any operator. | ||||||||||
|
||||||||||
]]></format> | ||||||||||
</remarks> | ||||||||||
</Docs> | ||||||||||
</Member> | ||||||||||
|
@@ -656,4 +662,4 @@ | |||||||||
</Docs> | ||||||||||
</Member> | ||||||||||
</Members> | ||||||||||
</Type> | ||||||||||
</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.
While it's true that this defined implicit operator accepts a
T
and not aT?
, it's also true that the compiler let's you assign null to aNullable<T>
, in which case it generates the equivalent of settingHasValue
tofalse
andValue
todefault
. From that perspective, what was written was correct. So I think the question here is whether the docs should be from the perspective of the actual raw API or the syntax the compiler enables; I'd be inclined to stick with what was there, since implicit operators are all about being used by the compiler. But I don't have a strong opinion; if you want to go with strictly what the API declares, then the new version looks fine.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.
I think that if you wanted to describe how the C# compiler converts a null literal to
T?
, it should be done in a separate paragraph that emphasizes how this implicit operator is not involved.