-
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: Add 'is not null' operator #1944
Comments
|
Pretty sure |
What's wrong with |
@willard720 |
Not until we do #1350 which is not currently expected to hit C# 8.0. |
@markusschaber True, but most people don't do an implicit conversion to bool, and I can't imagine what else would make sense for it. If someone did the code |
@willard720 It just might be confusing, and it might not be easy to resolve the conflicts with existing semantics ambigously. e. G. for Other languages have rather intuitive concepts of "truth value" (like Python, where thinks like 0, 0.0, And I doubt that being However, I'm afraid we're off-topic for this issue. :-( |
if(a is not null)
if(a != null) This ancient syntax is still better. for programmers its easy to catch. And what is |
If 'a' is not a local/parameter, there is no guarantee that it will be non-null, even after checking it against null. Specifically, another thread could end up changing it's value. 'Patterns', on the other hand, allow you to perform tests, then place the results in a local when successful. Thus giving you certainty about the state of the variable if the pattern succeeds in matching. |
@CyrusNajmabadi Im illuminated! 🌝 |
@RichardD2 I did not even know about those operators.... |
As @CyrusNajmabadi pointed out, this will be available in C# 8.0 though not with precisely the syntax you suggest. Supporting |
Surely The compiler even seems to correctly handle value types (generates a CS0183 warning, and omits the test from IL) and nullable value types (replaces the test with a call to Of course, it wouldn't work for |
|
Currently, we already have something like
The opposite case, the question if something is not null, is (asymmectrically) much harder to formulate. It would be nice to have something like
or ideally
so that
bnn
is then guaranteed to be not null in a thread safe way, would make life much easier.The text was updated successfully, but these errors were encountered: