DEV Community

Discussion on: Simplify foreach loops with the null-coalescing operator

devguyky profile image

My personal preference for more readable code would be to adopt a 'happy path' and return from the method before reaching the loop.

if (listOfStrings is null)

foreach (string item in listOfStrings)
{ ... }
Enter fullscreen mode Exit fullscreen mode

A good article on adopting the happy path