C# 6.0-8.0 Nullable Operators
Use nullable operators to simplify null checks.
So far, we covered that to avoid the NullReferenceException
:
- We should check for
null
before accessing the methods or properties of an object. - We should reject
null
from 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.
Get hands-on with 1400+ tech skills courses.