Following the guidelines of defensive programming and fail-fast system design, a method should always validate it’s input.

The code that validates your method’s inputs is called a Guard Clause. It makes your code more understandable and it protects you from bugs and unexpected behaviors.

For example, lets assume we have a simple method that validates a Url:

public bool IsUrlValid(string url)
{
    return Uri.TryCreate(url, UriKind.Absolute, out Uri result) && 
           (result.Scheme == "http" || result.Scheme == "https");
}

If we pass null as the url parameter then the method will return false. At first glance, this may seem ok, since null is indeed an invalid url. However this wasn’t our intention and we do not consider null a valid input value.

So we could rewrite the method as:

public bool IsUrlValid(string url)
{
    if(url == null) 
        throw new ArgumentNullException(nameof(url));
    return Uri.TryCreate(url, UriKind.Absolute, out Uri result) && 
           (result.Scheme == "http" || result.Scheme == "https");
}

That if expression is a Guard Clause. However we can do better than that, since repeating this code over and over in all of our methods is a bad practice. (DRY principle).

So lets create a helper class:

public static class GuardClauses
{
    public static void IsNotNull(object argumentValue, string argumentName)
    {
        if (argumentValue == null) 
            throw new ArgumentNullException(argumentName);
    }
}

And use it like this:

public bool IsUrlValid(string url)
{
    GuardClauses.IsNotNull(url,nameof(url));
    return Uri.TryCreate(url, UriKind.Absolute, out Uri result) && 
           (result.Scheme == "http" || result.Scheme == "https");
}

Note: I am usually against the usage of static methods. However in this case, I believe this a rare exception where a static method can be used since it is not affecting our code’s testability and it does not increase it’s complexity.

We can keep adding more Guard Clauses to our helper class as needed. Here are some more:

public static class GuardClauses
{
    public static void IsNotNull(object argumentValue, string argumentName)
    {
        if (argumentValue == null) 
            throw new ArgumentNullException(argumentName);
    }

    public static void IsNotNullOrEmpty(string argumentValue, string argumentName)
    {
        if (string.IsNullOrEmpty(argumentValue)) 
            throw new ArgumentNullException(argumentName);
    }

    public static void IsNotZero(int argumentValue, string argumentName)
    {
        if (argumentValue == 0) 
            //C# 7.0 syntax: $"Argument '{argumentName}' cannot be zero"
            throw new ArgumentException("Argument '" + argumentName +"' cannot be zero",
                                         argumentName);
    }

   public static void IsLessThan(int maxValue, int argumentValue, string argumentName)
   {
       if (argumentValue >= maxValue) 
            //C# 7.0 syntax: $"Argument '{argumentName}' cannot exceed '{maxValue}'"
            throw new ArgumentException("Argument '" + argumentName +"' cannot exceed " + maxValue,
                                         argumentName);
   }

   public static void IsMoreThan(int minValue, int argumentValue, string argumentName)
   {
       if (argumentValue <= minValue) 
            //C# 7.0 syntax: $"Argument '{argumentName}' cannot be lower than '{minValue}'"
            throw new ArgumentException("Argument '" + argumentName +"'  cannot be lower than " + minValue,
                                         argumentName);
   }
}

Note: Some may disagree with Guard Clauses like “IsLessThan” and “IsMoreThan” because they may step into business logic rules. It’s up to you to decide.