In this article, we’re going to talk about the difference between the usage of the “is” and equality operators when checking for a null
value.
Let’s dive in!
The “Is” Operator
The is
operator finds its main use in the comparison of whether an object or expression is of a given run-time type. The operator will also return true
when the compared object inherits from the checked type or implements the given interface.
If you want to learn more about the is
operator, please check out our article on As and Is Operators in C#.
Null Checking With “Is” Operator
From C# 7, we can check whether an object or expression is null
with the usage of the is
operator:
if (person is null) { throw new ArgumentNullException(nameof(person)); }
As mentioned in the Top 11 C# Tips to Improve Code Quality and Performance article, we recommend using the is
operator instead of the equality operator.
Why? Let’s start by explaining how both operators work.
The equality operator is part of the C# language and checks whether references are equal (whether they point to the same object).
The is
operator uses the ReferenceEquals
method:
public static bool ReferenceEquals (Object objA, Object objB) { return objA == objB; }
As you can see, there are not many differences. Under the hood, both options use the equality operator. The only (super important!) advantage is that is
ignores operator overloads. A programmer can always override the ==
operator on the class level, so there is a danger that our checks will stop working (or start working differently than we think).
Let’s have a look at an example where the equality operator is overridden. We want to compare, based on the path, whether the files are the same:
public class File { public string Path { get; } public File(string path) { Path = path; } public static bool operator == (File file1, File file2) { return file1.Path == file2.Path; } }
In a real project, if you want to override the equality operator, you should also do it for the inequality operator (!=)
as well as for the Equals
and GetHashCode
methods. To find out more read our article about Operator Overloading.
Comparing the files will work fine, we can easily determine that they are not the same:
File jpgPhoto = new("path1"); File pdfFile = new("path2"); if (jpgPhoto == pdfFile) { Console.WriteLine("The files are the same"); } else { Console.WriteLine("The files are NOT the same"); }
The problem arises when we want to check if the file is null
. The result of such a check will be a NullReferenceException
:
File wordFile = new("path3"); if (wordFile == null) { Console.WriteLine("Word file does not exist"); }
The “Is” Operator Negation
Now, we can safely say that usage of the is
operator has almost nothing but advantages, but it wasn’t so obvious before. One of the programmers’ arguments for not using the operator was unconvincing writing when negating:
if (!(age is null)) { Console.WriteLine($"Age: {age}"); }
It is definitely less readable. The solution appeared in version 9 of C#, where the is not
operator was introduced. To learn more, read our article about Differences Between != And Is Not Operators in C#.
Alternative to the Is Operator
It’s worth mentioning that C# 7 introduced the null-coalescing operator which also allows validating whether an object or expression is null
. Since the operator ??
it is not overwritable, it can be used with confidence, just like the is
operator:
_ = age ?? throw new ArgumentNullException(nameof(age));
Conclusion
In this article, we have learned what is the difference between the is
and ==
operators when checking if the value is null
.