C#: Different ways to Check for Null

What is the classic way to check if for example a parameter value is null? If you’ve developed with C# since a while, you might be familiar with this classic syntax:

public static int CountNumberOfSInName(string name)
{
  if (name == null)
  {
    throw new ArgumentNullException(nameof(name));
  }

  return name.Count(c => char.ToLower(c).Equals('s'));
}

Since C# version 7 you can use the is keyword for the null check like in the snippet below:

  if (name is null)
  {
    throw new ArgumentNullException(nameof(name));
  }

But with C# 7, there’s even a shorter syntax. Discards were also introduced. They are unused and ignored variables that are represented in your code with an underscore (_). In combination with the Null-coalescing operator (??) you can write the null check like this:

_ = name ?? throw new ArgumentNullException(nameof(name));

That means, the whole method looks just like this:

public static int CountNumberOfSInName(string name)
{
  _ = name ?? throw new ArgumentNullException(nameof(name));

  return name.Count(c => char.ToLower(c).Equals('s'));
}

To be honest, I really like the last approach using the discards, but maybe for some developers it’s too much. I think the is keyword is very clear and readable. It is my favorite.

The is keyword has also the big advantage that it ignores any ==/!= operator overloads on the specific class. It will do a null check, no matter if there’s an operator overload or not. That makes it better than just using ==.

Share this post

Comments (9)

  • Dew Drop – March 13, 2020 (#3153) | Morning Dew Reply

    […] C#: Different ways to Check for Null (Thomas Claudius Huber) […]

    March 13, 2020 at 12:03 pm
  • Joel Reply

    When you want to check for not-null, you can do..

    if (foo != null)
    if (!(foo is null))
    if (foo is object)

    I prefer the third because it’s clear and concise and doesn’t involve negation or nested parens.

    March 13, 2020 at 4:43 pm
    • Thomas Claudius Huber Reply

      Hi Joel,

      yes, that’s super cool. I really like “foo is object”, but I guess it’s not as obvious as !(foo is null)

      Best would be

      if(foo is not null)

      But we don’t have that (yet?).

      Thomas

      March 17, 2020 at 3:04 pm
  • The Morning Brew - Chris Alcock » The Morning Brew #2953 Reply

    […] C#: Different ways to Check for Null – Thomas Claudius Huber […]

    March 16, 2020 at 7:00 am
  • Dipo Reply

    Good use of _ = name ?? throw new ArgumentNullException(nameof(name));

    it’s sensible and neat

    March 16, 2020 at 9:11 am
  • Eric Bäckhage Reply

    I like the ’is’ variant. Hopefully, with the nullable reference types introduced in C#8 we’ll be able to get rid of null-guards like these.
    If there is a need to represent the possible abscence of a value I prefer the Option type. I wrote a blog post on this topic that you might find interesting, see https://ericbackhage.net/c/nullable-reference-types-compared-to-the-option-monad/

    March 17, 2020 at 6:25 am
    • Thomas Claudius Huber Reply

      Hi Eric,

      yes, with nullable reference types we should be able to reduce these null-guards in our code.

      Thank you for sharing that blog post, it’s a great read!
      Thomas

      March 17, 2020 at 3:06 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.