2 ms·
Sorry, I meant a null coalescing property access operator that returns null if the left hand side is null. Today you write something like this var street
by alkonaut 13y ago
Sorry, I meant a null coalescing property access operator that returns null if the left hand side is null.
Today you write something like this
var street = (user != null && user.Address != null) ? user.Address.Street : null;
When you could use something like this
var street = user?.Address?.Street;
- bruceboughton 13y agoEasily solved with an extension method; public static TReturn Try<TType, TReturn>(this TType value, Func<TType, TReturn> accessor) { if (value == null) return default(TReturn); return accessor(value); } The compiler should infer the generic types for you: var street = user.Try(_ => _.Address).Try(_ => _.Street); Sure the syntactic sugar is nicer but the extension method's not that much worse.
- alkonaut 13y agoYes, I just want the sugar. Also, must not be any slower than the if-then solution of course. If the func calls aren't inlined/removed I don't think it would be good enough perf wise.