-
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
Proposal: Permit 'value is null' if 'value' is unconstrained generic #1261
Comments
This change was first proposed by @gafter here.
I wrote the proposal because I couldn't find something related and I wanted to track this change for dotnet/roslyn#24173. |
Related proposal for unconstrained generic #263 |
Closed as duplicate of #1284 (Thanks @DavidArno for pointing that out). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently the
value is null
pattern does not compile ifvalue
is an unconstrained generic type:This proposal is about removing this restriction.
Motivation
The main benefit would be, that this brings
value is null
more in line withvalue == null
. Both expressions are almost interchangeable with two exceptions:value == null
calls a user-defined equality operator if present, whileis null
does not.value == null
is permitted ifvalue
is unconstrained generic, whileis null
is not allowed.The second difference can be removed. This would also bring
is null
more in line with VBsIs Nothing
which behaves as== null
.This language change would benefit code fix authors that deal with this kind of expressions and have to special case unconstrained generics (Recent examples are dotnet/roslyn#24237 and dotnet/roslyn#24173). It would also benefit users as it removes an artificial difference between otherwise (almost) identical constructs.
Proposed behavior for
value is null
value is null
would have to behave asvalue == null
(see also C# spec links below).Impact / Breaking changes
It is proposed to remove an existing compile time error. So this isn't breaking anything. Existing diagnostic analyzers are affected because CS0403 would no longer be reported (which in turn might cause unit tests to fail).
Some more background
The following snippets show the differences between
is null
,==null
and VBsIs Nothing
in respect to generic type parameter constraints.sharplab.io
shaprlab.io
There is only one case where the three comparisons substantial differ and this proposal is about removing this difference.
C# Spec references
The Reference type equality operators section from the spec about
== null
explicit allows T to be unconstrained. The section The is operator does not mention the
is null
case. The spec needs to adopt this part from the Reference type equality operators section:Two more quotes about the difference between user defined comparison:
Update: Oops! I got that quote wrong. It is about conversions and not comparison.
The text was updated successfully, but these errors were encountered: