?? and ??= operators for non-nullable types #9456
Unanswered
tvidvei
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I just had a case where I wish I had an operator similar to the "null-coalescing" operators ?? and ??==, which could be used also with non-nullable types:
Here I would like to write the getter like the following, using the pattern I would use with nullable types:
Couldn't the null-coalescing operators be generalized to 'default-coalescing' operators that could be used both with nullable and non-nullable types by testing for equality with default(T) where T is the actual type?
Such a change would not break any existing code as far as I can see as the ?? and ??= operators currently can't be used with non-nullable types.
My proposal is then the following:
This solution comprises the present semantics as 'default' is the same as null for nullable types.
Beta Was this translation helpful? Give feedback.
All reactions