.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) {     ArgumentNullException.ThrowIfNull(bar);     // do something else }  

Isn't that a whole lot cleaner ?


This post is ad-supported