3 ms·
Uhm http://en.wikipedia.org/wiki/Null_coalescing_operator http://en.wikipedia.org/wiki/Null_coalescing_operator First sample is C#?
by zidad 13y ago
Uhm http://en.wikipedia.org/wiki/Null_coalescing_operator http://en.wikipedia.org/wiki/Null_coalescing_operator
First sample is C#?
- alkonaut 13y agoSorry, 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.