blambert/codesnip – string extension method for validating that a string parameter is not null or empty.

I love extension methods.  Here’s a simple little string extension method:

/// <summary>
/// Validates that the input string is not null or empty.  If the input
/// string is null, ArgumentNullException will be thrown.  If the input
/// string is empty, ArgumentException will be thrown.
/// </summary>
/// <param name="value">The input string to validate.</param>
/// <param name="paramName">The name of the parameter being validated.</param>
private static void ValidateIsNotNullOrEmpty(this string value, string paramName)
    if (value == null)
        throw new ArgumentNullException(paramName);

    if (value.Length == 0)
        throw new ArgumentException("cannot be empty", paramName);

to validate string arguments.

Call it like:

public void SomeRoutine(string someStringParameter)


Skip to main content