Code inspection: Use 'ArgumentException.ThrowIfNullOrEmpty/ThrowIfNullOrWhiteSpace'
Starting from .NET 8.0, the ArgumentException class provides static methods ThrowIfNullOrEmpty() and ThrowIfNullOrWhiteSpace() to simplify argument validation.
This inspection identifies manual null or empty/whitespace checks followed by throwing an ArgumentException and suggests replacing them with these more concise and readable static methods. These methods not only reduce boilerplate code but also provide a consistent way of throwing exceptions for invalid arguments.
public void Method(string arg1)
{
if (string.IsNullOrWhiteSpace(arg1))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(arg1));
}
public void Method(string arg1)
{
ArgumentException.ThrowIfNullOrWhiteSpace(arg1);
}
The quick-fix replaces the if statement and the throw expression with a single call to ArgumentException.ThrowIfNullOrEmpty() or ArgumentException.ThrowIfNullOrWhiteSpace(), depending on the original check.
11 March 2026