-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Discussion: Always explicitly type null-conditional operator ?.
#417
Comments
The described behavior is by design and had already been argued at some depth on Codeplex. Changing it to return a non-nullable value type or to require an explicit cast would both be a breaking change and not make any sense. |
@HaloFour |
It certainly wouldn't hurt for the C# Guide to make that more explicit. The example code explicitly uses nullable value types, from which their non-nullable counterparts can be implicitly cast, so the code would compile either way. |
Thats not true. |
I might be wrong here, but it should work if you use int z2 = cA?.x ?? 12; instead of var z2 = cA?.x ?? 12; |
@zippec But you see, that is the 'confusion' I am talking about. I am surely not "dump" or "wanting" and believe that I am average when it comes to programming - you know, the middle of a Gaussian bell curve. When it sometimes confuses me, it surely isn't clear to others as well. That should be helped! @Unknown6656 |
There's no way we will do this as proposed, as it is a breaking change. |
Sure, that's why this is only a discussion and not a proposal 😃 |
I'm honestly not sure why playing around with the operator doesn't make everything intuitive. It's satisfying when your intuition that it should return a non-nullable is confirmed. It's like any language feature in that way. Since you know intuitively that null is impossible, it's nice that the compiler makes that knowledge type safe. This is like complaining that a hypothetical Math.Square returns a NonnegativeDouble instead of a double. No, that makes sense, because it's impossible for a squared real number to be negative. |
The intent for this discussion arised from an insufficient knowledge of the underlying processing and casting of the '?.' and '??' operators. Except from the C# language specification this knowledge is not easily gained from the common C# guide and help pages as well as from common C# learning books. My recommendation is to update the official guide and language help to give more insights on the conversions those operators do. |
When having a class like
the null-conditiona operator
?.
always converts the non-nullable type to a nullable-type:That has confused me already many, many times! Like in the following case:
Latter especially when it comes to the default-coalescing-operator
??
(#328), that I have implementedI'd like to discuss if a mandatory explicit cast could help to avoid the misunderstanding.
I would also like to point out, that the implicit conversion to Nullable stands in the specs, but not in the ordinary help pages (https://msdn.microsoft.com/en-us/library/dn986595.aspx).
The text was updated successfully, but these errors were encountered: