3 ms·
Easily solved with an extension method; public static TReturn Try<TType, TReturn>(this TType value, Func<TType, TReturn> accessor) { if (value
by bruceboughton 13y ago
Easily 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.