Last active
November 14, 2018 20:19
-
-
Save virtualstaticvoid/fffe3fd77e6d54ad47de180857cc90ae to your computer and use it in GitHub Desktop.
C# Ensure for Argument Errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MIT License | |
// Copyright (c) 2009 Chris Stefano <[email protected]> | |
// | |
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