During my career as a software engineer, I have analyzed millions of lines of code for issues, and one of the top things that I see developers not doing is validating data coming into their types. This includes methods and properties. I understand why. It’s mundane work, but it’s very important since bad data in equals bad data out. If that bad data gets into the database, then it’s very difficult to fix. Also, if the code is not validating the data, then it’s not properly implementing encapsulation and therefore is not proper object-oriented programming.
In previously released versions of Spargine, I have had helper methods for this. For my work moving Spargine to .NET 6, I have completely rewritten these methods to make it even easier to validate data using fluent validation. I wish that the .NET team at Microsoft would add helper methods like this to .NET, but until then teams can use the extension methods in the Validator class in dotNetTips.Spargine.Core. The source code and NuGet packages can be found below.
- GitHub: https://github.com/RealDotNetDave/dotNetTips.Spargine/releases
- NuGet: http://bit.ly/dotNetDaveNuGet
Validating Methods and Properties
I will document all the methods currently available to validate parameters and show examples. All these methods return the data that is being validated so you can use these methods with others. Most also support a default value to be returned if validation fails. All the Exceptions thrown in these methods properly use ArgumentException or another exception that uses this class as its base class. If a custom error message is not supplied, then a default one will be used.
All the methods below have a parameter called paramName, but there is no need to use it. The methods automatically get the name by using the CallerArgumentExpression attribute.
Checking For Null
Before operating on any data from a call to a database or an API, etc., it’s very important to make sure it’s not null. This is a very common Exception that I see all the time in logs or bug reports. For this validation, you can use ArgumentNotNull(). This method supports <T> and Uri. Here is an example of how to use this method.
public static async Task<string> DownloadStringAsync(Uri address)
{
address = address.ArgumentNotNull();
// Code removed for brevity
}
Validating Data is Not Null and Not Empty
To make sure that data is not null and not empty, you can use the ArgumentNotNullEmpty(). Currently, this method is overloaded for use with Guid and string. Here is an example of how to use it for a string.
internal static bool IsEmailAddress(this string input)
{
return input.ArgumentNotNullOrEmpty().HasValue(Resources.RegexEmail, RegexOptions.IgnoreCase);
}
Validating Data with a Condition
If you need to validate data using a Boolean condition, you can use ArgumentMeetsCondition().
public static byte[] GenerateByteArray(double sizeInKb)
{
sizeInKb = sizeInKb.ArgumentMeetsCondition(sizeInKb >= double.Epsilon, errorMessage: $"Size must be >= {double.Epsilon}.");
// Code removed for brevity
}
Validating Data with a Regular Expression
If you need to validate data with a regular expression, then you can use the ArgumentMatched() method as shown below.
var expression = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
userEmail = userEmail.ArgumentMatched(expression);
Validating Collections
To ensure a collection has items in it before you try to use it, you can use ArgumentItemsExist(). Far too many times I’ve seen code like this, even today.
var person = personCollection[2];
If there aren’t items in the personCollection or it’s null, then this will cause an exception. This overloaded method supports IEnumerable<T>, IList<T>, and T[]. Here is an example of how to use ArgumentItemsExist().

Join the conversation! Your thoughts help the community grow.