-
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
Null check syntactic sugar; Make foo?
equivalent to foo != null
#833
Comments
If |
.. has already been suggested a few times .. See:
Possible syntax (unary prefix operator with highest precedence):
The static public bool operator ? (T t) where T : class => !ReferenceEquals (null, t); For details about this particular syntax see the two linked threads. Both threads mentioned above are closed right now, but I still think a null check operator would be really nice. |
@HaloFour : I think a prefix instead of postfix operator would avoid the problems you mention. |
My opinion is that an equality comparison is succinct enough and significantly more readable than either operator-based proposal. |
So here's my weekend idea...
Null checks are common so why not sprinkle on some (more) syntactic sugar?
Instead of
how about
We've already become accustom to an expression that starts with
myArg?
being declarative syntax for a null check followed by property access (as inmyArg?.Prop
) or a null check followed by an alternate expression (as inmyArg ?? myOtherArg
). So ifmyArg?
is followed by nothing, then... well... it's just a null check resulting in a bool (which could possibly be followed by some procedural action like throwing an exception).The text was updated successfully, but these errors were encountered: