An extendable guard implementation.
A guard clause helps to check inputs for validity and fails immediately if any invalid inputs are found. The guards are implemented as extension methods on the marker IGuard. This way custom guards
can be added to the available default guard clauses.
Check inputs of methods and constructors and fail early. The guard will fail with an exception if the input is not valid.
public void UpdateCustomer(Customer customer)
{
    Guard.Against.Null(customer, nameof(customer));
    // Update the customer...
}Check inputs constructors, fail early or assign the value. The guard will fail with an exception if the input is not valid or return the input value if it is valid.
public class Customer
{
    public Customer(string name, decimal credit)
    {
        this.Name = Guard.Against.NullOrWhiteSpace(name, nameof(name));
        this.Credit = Guard.Against.Negative(credit, nameof(credit));
    }
    public string Name { get; }
    public decimal Credit { get; }
}Create your own guards by adding extension methods on IGuard. To create the exception to throw you can use one of the helpers from the ExceptionHelpers class. This helpers make sure that the correct constructors are used and the parameter name is set correctly.
// ReSharper disable once CheckNamespace
namespace Fluxera.Guards
{
    // Note: Using the namespace 'Fluxera.Guard' will ensure that your
    //       custom guard is available throughout your projects.
    using JetBrains.Annotations;
    using static ExceptionHelpers;
    public static class CustomGuardExtensions
    {
        public static void Hello(this IGuard guard, string input, string parameterName, string? message = null)
        {
            if(input.ToLower() == "hello")
            {
                throw CreateArgumentException(parameterName, message);
            }
        }
    }
}Guard.Against.Null- Throws an 
ArgumentNullExceptionif the input isnull. 
- Throws an 
 Guard.Against.Default- Throws an 
ArgumentExceptionif the input isdefault. 
- Throws an 
 Guard.Against.NullOrEmpty- Throws an 
ArgumentNullExceptionif the input string isnull. - Throws an 
ArgumentExceptionif the input string isstring.Empty. - Throws an 
ArgumentNullExceptionif the input enumerable isnull. - Throws an 
ArgumentExceptionif the input enumerable is empty. - Throws an 
ArgumentNullExceptionif the input nullable guid isnull. - Throws an 
ArgumentExceptionif the input nullable guid isGuid.Empty. 
- Throws an 
 Guard.Against.NullOrWhiteSpace- Throws an 
ArgumentNullExceptionif the input string isnull. - Throws an 
ArgumentExceptionif the input string isstring.Emptyor whitespace-only. 
- Throws an 
 Guard.Against.Empty- Throws an 
ArgumentExceptionif the input guid isGuid.Empty. 
- Throws an 
 Guard.Against.Negative- Throws an 
ArgumentExceptionif the input is < 0. 
- Throws an 
 Guard.Against.Zero- Throws an 
ArgumentExceptionif the input is == 0. 
- Throws an 
 Guard.Against.NegativeOrZero- Throws an 
ArgumentExceptionif the input is <= 0. 
- Throws an 
 Guard.Against.OutOfRange- Throws an 
ArgumentOutOfRangeExceptionif the input is not within a given range. - Throws an 
InvalidEnumArgumentExceptionif the input is not a defined enum value. 
- Throws an 
 Guard.Against.False- Throws an 
ArgumentExceptionif the input isfalse. 
- Throws an 
 Guard.Against.True- Throws an 
ArgumentExceptionif the input istrue. 
- Throws an 
 Guard.Against.InvalidInput- Throws an 
ArgumentExceptionif the input doesn't satisfy a predicate. 
- Throws an 
 Guard.Against.InvalidFormat- Throws an 
ArgumentExceptionif the input doesn't match a regex pattern. 
- Throws an