Created
November 10, 2022 21:24
-
-
Save nikanos/861abeeeb6a538b40011ab5e855c9169 to your computer and use it in GitHub Desktop.
C# class for method parameter checks
This file contains 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
public static class Ensure | |
{ | |
public static void ArgumentNotNull<T>(T argument, string argumentName) where T : class | |
{ | |
if (argument == null) | |
throw new ArgumentNullException(argumentName); | |
} | |
public static void ArgumentNotNull<T>(Nullable<T> argument, string argumentName) where T : struct | |
{ | |
if (argument == null) | |
throw new ArgumentNullException(argumentName); | |
} | |
public static void StringArgumentNotNullAndNotEmpty(string argument, string argumentName) | |
{ | |
if (argument == null) | |
throw new ArgumentNullException(argumentName); | |
if (argument.Length == 0) | |
throw new ArgumentException("argument cannot be empty", argumentName); | |
} | |
public static void ArrayArgumentNotNullAndNotEmpty<T>(T[] argument, string argumentName) | |
{ | |
if (argument == null) | |
throw new ArgumentNullException(argumentName); | |
if (argument.Length == 0) | |
throw new ArgumentException("argument cannot be empty", argumentName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment