Skip to content

Instantly share code, notes, and snippets.

@nikanos
Created November 10, 2022 21:24
Show Gist options
  • Save nikanos/861abeeeb6a538b40011ab5e855c9169 to your computer and use it in GitHub Desktop.
Save nikanos/861abeeeb6a538b40011ab5e855c9169 to your computer and use it in GitHub Desktop.
C# class for method parameter checks
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