C# 6.0-8.0 Nullable Operators
Use nullable operators to simplify null checks.
We'll cover the following...
We'll cover the following...
So far, we covered that to avoid the NullReferenceException:
- We should check for
nullbefore accessing the methods or properties of an object. - We should reject
nullfrom our methods when they require some parameters.
Let’s look at the new operators C# introduced to simplify our null checks: ?., ??, and ??=. These operators don’t prevent us from having null in the first place, but they help us simplify our work of checking for null values.
Without nullable operators
Let’s start with an example and refactor it to use these new operators.
Let’s say we want to find one movie from our catalog to follow its director.
Since the result from FindMovie() might be ...