Value Guards

You can use this class to easily validate values being passed into your methods. For example, you would typically use something like this to check your method arguments

public void ValidateUserInput(int amount, string name)
{
	if (amount == 0)
	{
		throw new ArgumentOutOfRangeException(nameof(amount));
	}
	
	if (string.IsNullOrEmpty(name))
	{
		throw new ArgumentException(nameof(name));
	}
}

Using the class below you can express the same thing like this.

public void ValidateUserInput(int amount, string name)
{
	amount.ThrowIfZero(nameof(amount));
	name.ThrowIfNullOrEmpty(nameof(name));
}
   using System;
   using System.Diagnostics;
   using System.Linq.Expressions;

   public static class ValueGuard
   {
      [DebuggerStepThrough]
      public static void ThrowIfZero(this int value, string name)
      {
         if (value.Equals(0))
         {
            throw new ArgumentOutOfRangeException(name, value, "Value cannot be zero");
         }
      }

      [DebuggerStepThrough]
      public static void ThrowIfNegative(this int value, string name)
      {
         if (value < 0)
         {
            throw new ArgumentOutOfRangeException(name, value, $"Value cannot be negative");
         }
      }

      [DebuggerStepThrough]
      public static void ThrowIfLessThanOrEqualTo(this int value, int minValue, string name)
      {
         if (value <= minValue)
         {
            throw new ArgumentOutOfRangeException(name, value, $"Value cannot be less than or equal to {minValue}");
         }
      }

      [DebuggerStepThrough]
      public static void ThrowIfNull(this object value, string name)
      {
         if (value == null)
         {
            throw new ArgumentNullException(name);
         }
      }

      [DebuggerStepThrough]
      public static void Throw<T>(this object value, Expression<Func<object, bool>> expression, string name)
        where T : Exception, new()
      {
         if (!expression.Compile().Invoke(value))
         {
            T exception = new T
            {
               Source = name
            };

            throw exception;
         }
      }

      [DebuggerStepThrough]
      public static void ThrowIfNullOrEmpty(this string value, string name)
      {
         if (string.IsNullOrEmpty(value))
         {
            throw new ArgumentNullException(name);
         }
      }

      [DebuggerStepThrough]
      public static void ThrowIfNullEmptyOrWhitespace(this string value, string name)
      {
         value.ThrowIfNullOrEmpty(name);

         if (string.IsNullOrWhiteSpace(value))
         {
            throw new ArgumentException(name);
         }
      }

      [DebuggerStepThrough]
      public static bool IsNull(this object value)
      {
         return value == null;
      }

      [DebuggerStepThrough]
      public static bool IsDefault<T>(this T value)
         where T : struct
      {
         bool isDefault = value.Equals(default(T));

         return isDefault;
      }

      [DebuggerStepThrough]
      public static bool IsEmpty(this string value)
      {
         return value == string.Empty;
      }

      [DebuggerStepThrough]
      public static bool IsNullOrEmpty(this string value)
      {
         return string.IsNullOrEmpty(value);
      }
   }
}