Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Last active November 14, 2018 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualstaticvoid/fffe3fd77e6d54ad47de180857cc90ae to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/fffe3fd77e6d54ad47de180857cc90ae to your computer and use it in GitHub Desktop.
C# Ensure for Argument Errors
//
// MIT License
// Copyright (c) 2009 Chris Stefano <virtualstaticvoid@gmail.com>
//
using System;
using System.Diagnostics;
internal static class Ensure
{
[DebuggerStepThrough]
public static void ArgumentNotNull(object argument, string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(argumentName);
}
}
[DebuggerStepThrough]
public static void ArgumentNotNull(string argument, string argumentName)
{
if (String.IsNullOrEmpty(argument))
{
throw new ArgumentNullException(argumentName);
}
}
[DebuggerStepThrough]
public static void ArgumentInRange<T>(T argument, string argumentName, T min, T max)
where T : IComparable
{
if (argument.CompareTo(min) < 0 || argument.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(argumentName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment