Replace a Set of Choices with FirstOrDefault

Use the FirstOrDefault method to replace a chain of conditionals.

How to Refactor a chain of conditionals with FirstOrDefault

When we need to find a value between multiple choices, we write consecutive if statements to check for a valid result every time.

For example, let’s find the next film to watch from one of three sources: the latest releases in cinemas, our friends’ recommendations, and the all-time classics.

Get hands-on with 1000+ tech skills courses.