Skip to content
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

Shortcut for "object == null" #2525

Closed
reschjo opened this issue May 14, 2019 · 6 comments
Closed

Shortcut for "object == null" #2525

reschjo opened this issue May 14, 2019 · 6 comments

Comments

@reschjo
Copy link

reschjo commented May 14, 2019

I very oftend use "if(Object == null) or if(Object != null)" in my sourcecode.
It would be great to have a shortcut like "if(?Object)" for that.
The return value would be "true" if the Object exists (not null) or "false" if the object doesn't exist (is null)

@tpetrina
Copy link

For the former you can use if (o is null). For the latter use if (o is object)

@HaloFour
Copy link
Contributor

Or if (o is {}) which will work for checking that o is not null in C# 8.0 when that is released

@DavidArno
Copy link

And of course, when we get nullable reference types with C# 8, the need to scatter if (o is null) throughout the code should largely go away.

@VanKrock
Copy link

You can to use ?? ?: ?.

@yaakov-h
Copy link
Member

Duplicate of #545, which was closed.

@reschjo reschjo closed this as completed May 15, 2019
@reschjo
Copy link
Author

reschjo commented May 15, 2019

@yaakov-h you are right. I closed the duplicated issue.

ToAll: Thanks for your answers !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants