RentADeveloper.ArgumentNullGuards 1.0.1

dotnet add package RentADeveloper.ArgumentNullGuards --version 1.0.1
                    
NuGet\Install-Package RentADeveloper.ArgumentNullGuards -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RentADeveloper.ArgumentNullGuards" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RentADeveloper.ArgumentNullGuards" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="RentADeveloper.ArgumentNullGuards" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RentADeveloper.ArgumentNullGuards --version 1.0.1
                    
#r "nuget: RentADeveloper.ArgumentNullGuards, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RentADeveloper.ArgumentNullGuards@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RentADeveloper.ArgumentNullGuards&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RentADeveloper.ArgumentNullGuards&version=1.0.1
                    
Install as a Cake Tool

NuGet Version license semver

image icon ArgumentNullGuards

A simple .NET library that verifies constructors and methods have guards in place against null arguments.

Inspired by a brilliant idea from Thomas Levesque, see https://thomaslevesque.com/2019/11/19/easy-unit-testing-of-null-argument-validation-c-8-edition/.

Table of contents

Installation

First, install NuGet.

Then install the NuGet package from the package manager console:

PM> Install-Package RentADeveloper.ArgumentNullGuards

Quick start

Use the ArgumentNullGuardVerifier class to verify that your constructors and methods properly guard against null arguments.
Call the Verify method, passing in a lambda expression that invokes the constructor or method you want to test with valid (non-null) arguments.

If any argument is not properly guarded against null, an exception will be thrown, causing the test to fail.
If an ArgumentNullException is thrown, but with the wrong parameter name, the test will also fail.

using static RentADeveloper.ArgumentNullGuards;

class UserService
{
	private readonly IUserRepository userRepository;
	
	public UserService(IUserRepository userRepository)
	{
		ArgumentNullException.ThrowIfNull(userRepository);
		this.userRepository = userRepository;
	}

	public IUserPermission[] GetPermissions(User user)
	{
		ArgumentNullException.ThrowIfNull(user);
		...
	}
}

public class UserServiceTests
{
	[Fact]
	public void VerifyNullArgumentGuards()
	{
		var userRepository = Substitute.For<IUserRepository>();
		var user = Substitute.For<User>();

		ArgumentNullGuardVerifier.Verify(() => new UserService(userRepository));
		
		var instance = new UserService(userRepository);

		ArgumentNullGuardVerifier.Verify(() => instance.GetPermissions(user));
	}
}

Contributing

Contributions and bug reports are welcome and appreciated.
Please follow the repository's CONTRIBUTING.md and code style.
Open a GitHub issue for problems or a pull request with tests and a clear description of changes.

License

This library is licensed under the MIT license.

Documentation

Full API documentation is available here.

Change Log

The change log is available here.

Contributors

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 102 12/30/2025
1.0.0 90 12/30/2025

Fixed icon URL in README.md.