Table of Contents

Using Validate

The static methods in Validate throw an exception if the value doesn't exist, or sometimes if it doesn't exist in the expected form. They can be used to null-check a parameter in a single line of code without repeating the same few lines throughout your codebase.

If the value does exist, Validate's methods return it untouched - so they can be used for pass-through validation where you wouldn't want to write lines of code at all, such as when passing arguments to a base or chained constructor:

public class ExampleClass : ExampleBaseClass
{
    public ExampleClass(string inText)
        : base(Validate.ThrowIfNull(inText))
    {
    }
}

Validate's null-checking methods can be used to resolve Code Analysis warning CA1062: Validate arguments of public methods.

The following methods won't resolve CA1062, but may also be useful: