.Net 6 : ArgumentNullException.ThrowIfNull

.Net 6 has some nice little hidden features which helps in creating cleaner code, and in this post we will look into one such feature.

Previously, if we needed to check if an object is null and raise an exception, we would have done as the following.

void Foo(Bar bar)
    if (bar is null)
        throw new ArgumentNullException();

    // do something else

This code could be now simplified by using the static method ArgumentNullException.ThrowIfNull. For example, rewriting the code in .Net 6,

void Foo(Bar bar)
    // do something else

Isn’t that a whole lot cleaner ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s