Proposal: null conditional + lambda expression #1676
Replies: 5 comments
-
This could be covered by e.g. #1661. |
Beta Was this translation helpful? Give feedback.
-
The idea behind the likes of #1661 is that a ArgumentNullException` is thrown, which is orthogonal to this proposal. Having said that, a proposal for shortcut syntax for only reassigning a value if it's not null is the complete opposite of "would be good to have". I just don't get why such logic would ever exist, much less want a shorthand way of expressing it. |
Beta Was this translation helpful? Give feedback.
-
Not to mention that OP's request can already be achieved in a different manner. //Add method on your 'Home' type:
public HomeSummary CreateSummary() => new HomeSummary(this);
//Call like so
var h = home?.CreateSummary(); Problem solved, no new syntax necessary. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno: #1661 is not about throwing an exception as you claim, but about skipping method execution if any one of the marked arguments is |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, It would be good to have something like
home?=> new PlayerSummary(home)
for scenario
var h = home != null ? new HomeSummary(home) : null
so instead of writing ternary operator of if/else we could just write null conditional + return lambda expression.
var h = home?=> new HomeSummary(home).
Cheers
Ali, Murtaza
Beta Was this translation helpful? Give feedback.
All reactions