Wednesday, July 29, 2015

Use safe navigation operator in C# 6.0 to check to see if something is null in advance of calling a method.

Here we check to see if foo is null before calling .GetBarList() and if it's null we just return a new empty list of Bar as suggested by the null coalescing operator.

return foo?.GetBarList() ?? new List<Bar>();

No comments:

Post a Comment