This package was created by Georgia EPD-IT to provide simple guard clause methods for our web applications.
This package was inspired by the great GuardClauses package by Steve Smith, which has a lot more options and extensibility.
To install, search for "GaEpd.GuardClauses" in the NuGet package manager or run the following command:
dotnet add package GaEpd.GuardClauses
Guard clauses simplify checking for invalid input parameters.
Example usage:
public class SomeClass
{
private readonly string _name;
public SomeClass(string name)
{
_name = Guard.NotNullOrWhiteSpace(name);
}
}
Each clause returns the original value if the conditions are met; otherwise, it throws an exception.
NotNull
– ensures a value is not null.NotNullOrWhiteSpace
– ensures a string is not null, empty, or whitespace.ValidLength
– ensures a string has a length between the specified minimum and maximum (inclusive).NotNegative
– ensures an integer is not negative.Positive
– ensures an integer is not zero or negative.RegexMatch
– ensures a string matches the provided regex pattern.