Exercise: LINQ XOrDefault Methods

Use FirstOrDefault() with safe defaults.

NullReferenceException and FirstOrDefault()

Now, let’s practice preventing a NullReferenceException when working with LINQ XOrDefault methods.

For this exercise, the Movie class contains a genre and some reviews. And the new Review class has a score and a comment. This time, we have a list of action movies and comedies with good and bad ratings.

To complete this exercise, think of more than one way of fixing a NullReferenceException. Feel free to use plain null checks, nullable operators, XOrDefault() with safe defaults, or XOrNone().

Get hands-on with 1200+ tech skills courses.