This library can be used to check method parameters' values and throw appropriate exceptions to avoid confusing NullReferenceException in any C#, .NET application.
If you find this library useful to you, please don't forget to encouraging me to do such more stuffs by giving a star (⭐) to this repository. Thank you.
First install the lastest version of TanvirArjel.ArgumentChecker
nuget package into your project as follows:
Install-Package TanvirArjel.ArgumentChecker
public class EmployeeService
{
public async Task CreateAsync(Employee employee)
{
employee.ThrowIfNull(nameof(employee));
...
};
public async Task GetByIdAsync(int employeeId)
{
employeeId.ThrowIfZeroOrNegative((nameof(employeeId));
...
};
}
- ThrowIfNull - Throws ArgumentNullException if parameter value is
null
. - ThrowIfNullOrEmpty - Throws ArgumentNullException if parameter value is
null
and throws ArgumentException if parameter value isempty or whitespace
. - ThrowIfOutOfLength - Throws ArgumentOutOfRangeException if parameter value is out of the specified length range.
- ThrowIfNotValidEmail - Throws ArgumentException if parameter value is not a valid email.
- ThrowIfNull - Throws ArgumentNullException if collection parameter value is
null
. - ThrowIfNullOrEmpty - Throws ArgumentNullException if collection parameter value is
null
and throws ArgumentException if collection isempty
.
- ThrowIfNull - Throws ArgumentNullException if parameter value is
null
.
- ThrowIfEmpty - Throws ArgumentException if parameter value is a
empty
guid.
- ThrowIfNullOrEmpty - Throws ArgumentNullException if parameter value is
null
guid and throws ArgumentException if parameter value is aempty
guid.
- ThrowIfZeroOrNegative - Throws ArgumentOutOfRangeException if parameter value is zero or negative.
- ThrowIfNegative - Throws ArgumentOutOfRangeException if parameter value is negative.
- ThrowIfOutOfRange - Throws ArgumentOutOfRangeException if parameter value is out of the specified range.
- ThrowIfOutOfRange - Throws ArgumentOutOfRangeException if parameter value is out of the specified range.