Skip to footer content
Iron Academy Logo
What's New in C# and .NET

Null Parameter Checks in C# 10 and .NET 6 In 10 Minutes or Less

Tim Corey
4m 49s

When working in C#, it’s common for developers to encounter null arguments being passed into methods. This can lead to unexpected behavior, runtime errors, and even unhandled exceptions if not properly handled. In his video “Null Parameter Checks in C# 10 and .NET 6 in 10 Minutes or Less,” Tim Corey explains how C# 10 has simplified handling of null values using the ArgumentNullException class.

In this article, we’ll walk through the video exactly as Tim explains it, to understand how null checks work and how they’ve improved in modern C#.

Setting the Stage – Checking for Null Parameters

Tim begins by creating a simple .NET 6 console application to show how null parameter checks are handled in C#. He clears the boilerplate code and declares a nullable string variable:

string? info = null;
string? info = null;

This null object represents a scenario where a method call might receive an argument that wasn’t properly instantiated. Tim then defines a simple method:

void SayHi(string message)
{
    Console.WriteLine($"Hello {message}");
}
void SayHi(string message)
{
    Console.WriteLine($"Hello {message}");
}

He passes the info variable to the method:

SayHi(info);
SayHi(info);

When the program executes, the output simply displays:

Hello

No exception occurs because the string concatenation accepts a null value and treats it like an empty string. But, as Tim points out, in most real-world methods, this can cause error-prone situations where a null reference leads to downstream issues or a NullReferenceException later in the call stack.

To make robust code, developers should validate arguments and ensure that any method expects valid, non-null input.

Traditional Null Check Before C# 10

Tim explains that before C# 10, the good practice was to manually check parameters and throw an ArgumentNullException when needed. Inside the SayHi method, he adds:

if (message is null)
    throw new ArgumentNullException(nameof(message));
if (message is null)
    throw new ArgumentNullException(nameof(message));

This check ensures that if a null argument is passed, the program immediately throws a new instance of the ArgumentNullException class.

When Tim runs the code, the error message clearly shows:

System.ArgumentNullException: Value cannot be null. (Parameter 'message')
System.ArgumentNullException: Value cannot be null. (Parameter 'message')

This unhandled exception indicates that the method received an invalid argument. The parameter name—in this case, 'message'—is automatically displayed in the output, helping the developer identify exactly which argument caused the issue.

Tim notes that this manual check works perfectly well in the .NET Framework and earlier .NET Core versions. However, it’s verbose and repetitive when dealing with multiple parameters. Each additional parameter that needs validation adds three or four lines of code, cluttering the method body.

The Simplified Approach in C# 10 – ArgumentNullException.ThrowIfNull()

At this point, Tim introduces the modern C# 10 syntax. Instead of writing multiple lines for each parameter, developers can now write a single line null check:

ArgumentNullException.ThrowIfNull(message);
ArgumentNullException.ThrowIfNull(message);

Tim explains that this one-liner internally performs the same validation logic. If the argument passed is null, the method throws an exception automatically—just like before—but with much cleaner syntax.

When the program executes again, the following output appears:

System.ArgumentNullException: Value cannot be null. (Parameter 'message')
System.ArgumentNullException: Value cannot be null. (Parameter 'message')

The behavior remains identical, but now the syntax is simplified. There’s no need to manually instantiate a new ArgumentNullException object using a constructor or specify the paramName parameter manually. The caller’s parameter name is automatically inferred by the compiler.

Tim emphasizes that this improvement saves time and reduces the chance of human error when referencing the wrong parameter name.

Example: Multiple Parameters

Tim then compares the traditional method with the new one when handling multiple parameters.

Before C# 10, a developer might write:

if (name is null)
    throw new ArgumentNullException(nameof(name));
if (email is null)
    throw new ArgumentNullException(nameof(email));
if (password is null)
    throw new ArgumentNullException(nameof(password));
if (name is null)
    throw new ArgumentNullException(nameof(name));
if (email is null)
    throw new ArgumentNullException(nameof(email));
if (password is null)
    throw new ArgumentNullException(nameof(password));

With the C# 10 enhancement, the same checks can now be written more succinctly:

ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(email);
ArgumentNullException.ThrowIfNull(password);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(email);
ArgumentNullException.ThrowIfNull(password);

This improvement isn’t just about fewer lines—it makes your methods cleaner, more readable, and easier to maintain. Tim notes that if your method expects three parameters, you now need only three null checks, rather than twelve lines of code.

Why This Matters – Safer, More Robust Code

Tim points out that while manually passing null to a string in a demo might not be realistic, it represents real-world problems. APIs, user inputs, deserialization processes, or object-returned operations might yield null objects unexpectedly.

By adding null checks at the start of a method, you prevent unexpected behavior and fail fast—meaning the program throws an exception early in the call stack before the issue propagates.

He also mentions that this new syntax helps you maintain consistent error handling patterns across your methods. It encourages writing robust code that’s resilient against invalid input and uninstantiated objects.

ArgumentNullException Details

Tim highlights that ArgumentNullException is part of the System namespace and inherits from the ArgumentException class. It’s thrown when a null reference is passed to a method that doesn’t accept it as a valid argument.

It typically includes:

  • The parameter name that caused the issue.

  • A clear error message: “Value cannot be null.”

  • The call stack, showing where the exception occurred.

Tim’s example demonstrates how .NET 6 continues to follow this behavior while improving the syntax.

He also briefly notes that developers can combine these checks with other features like the null-coalescing operator (??) or default values to provide fallback logic—for example:

message ??= "Default message";
message ??= "Default message";

This allows a method to assign a default value instead of throwing, if desired.

Importance of Null Checking in Error Handling

Tim reminds developers that even though C# now supports nullable reference types, runtime null checks are still essential. The compiler can’t catch every null reference issue—especially when data comes from external sources or methods outside your control.

He stresses that it’s good practice to check your arguments and throw an ArgumentNullException when appropriate, ensuring that the method only executes when the inputs are valid. This minimizes runtime errors, simplifies error handling, and prevents silent failures.

Wrapping Up and Looking Ahead

As Tim concludes, he notes that C# 11 will introduce even more enhancements, but for now, ArgumentNullException.ThrowIfNull() is one of the best small improvements for error handling and code safety in .NET 6.

He encourages developers to try this approach in their own projects and see how much cleaner their methods look. He ends with a question: “What are your thoughts on this change in C# 10?”

Final Thoughts

Tim Corey’s video makes it clear that the ArgumentNullException.ThrowIfNull() method is a simple yet powerful improvement in C# 10. It reduces error-prone manual code, ensures valid arguments, and makes your programs fail fast when a null argument is passed.

By using this method consistently, developers can build robust, readable, and maintainable code that handles exceptions cleanly and avoids subtle null reference bugs.

In short, whenever your method expects a valid parameter and you want to prevent null arguments, follow Tim Corey’s example and use:

ArgumentNullException.ThrowIfNull(parameterName);
ArgumentNullException.ThrowIfNull(parameterName);

It’s a cleaner, safer, and more modern way to protect your code from null values and runtime errors in .NET 6 and beyond.

Hero Worlddot related to Null Parameter Checks in C# 10 and .NET 6 In 10 Minutes or Less
Hero Affiliate related to Null Parameter Checks in C# 10 and .NET 6 In 10 Minutes or Less

Earn More by Sharing What You Love

Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!