klucyszy
klucyszy

Reputation: 180

Should I use == or 'is' to check for null in C# 7?

from the C# 7 we have available is pattern matching operator. I would like to ask if it is recommended to use is operator rather than == to check for null. Is there any difference between these two approaches?

Upvotes: 2

Views: 190

Answers (1)

Julien Couvreur
Julien Couvreur

Reputation: 4963

== can be customized on your type, so x == null may not be just the intended null check.

For a null check, you could do x is null, (object)x == null or object.ReferenceEquals(x, null).

Upvotes: 3

Related Questions