Preconditions.NET 2.0.0

Install-Package Preconditions.NET -Version 2.0.0
dotnet add package Preconditions.NET --version 2.0.0
<PackageReference Include="Preconditions.NET" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Preconditions.NET --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Preconditions.NET, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Preconditions.NET as a Cake Addin
#addin nuget:?package=Preconditions.NET&version=2.0.0

// Install Preconditions.NET as a Cake Tool
#tool nuget:?package=Preconditions.NET&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Preconditions.NET AppVeyor build status NuGet

<img align="right" width="128px" height="128px" src="">


Preconditions.NET provides convenience static methods to help check that a method or a constructor is invoked with proper parameter or not. In other words it checks the pre-conditions. The goal of this class is to improve readability of code.

Preconditions.NET returns the tested value on success, allowing to check and call a method at the same time.

On failure it always throws an ArgumentException, ArgumentNullException or ArgumentOutOfRangeException.

Why Preconditions.NET v2 ?

Preconditions.NET version 2 is a complete rewrite that takes advantage of the new .NET 6 CallerArgumentExpression attribute. It is no more mandatory to pass the parameter name being checked. It also adds new methods (cf. checklist) and remove Check.Zero().

How to use it ?

Because Preconditions.NET is only one code file, you can either copy the Check.cs class or include the NuGet package to your project :

PM> Install-Package Preconditions.NET


public class Employee : Person
    public Employee(string name, string id) : base(Check.NotNullOrEmpty(name))
        Id = Check.NullableButNotEmpty(id);

    public string Id { get; }

The Checklist

  • Check.NotNull(object)
  • Check.NullableButNotEmpty (string)
  • Check.NotNullOrEmpty(string)
  • Check.NotNullOrEmpty(IEnumerable)
  • Check.HasNoNulls(IEnumerable)
  • Check.NotEmpty(Guid)
  • Check.FileExists()
  • Check.DirectoryExists()
  • Check.Positive()
  • Check.PositiveOrNull()
  • Check.Negative()
  • Check.NegativeOrNull()
  • Check.NotNegative()
  • Check.NotNegativeOrNull()
  • Check.True(Func<bool>)

Feedback and issues

Feedback, improvements, ideas are welcomed. Feel free to create new issues at the issues section.

  • .NETStandard 2.0

    • No dependencies.
  • net6.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
2.0.0 47 1/4/2022
1.4.0 17,571 11/18/2017
1.3.1 36,700 10/23/2017
1.3.0 845 7/18/2017
1.2.1 670 5/17/2017
1.2.0 639 4/29/2017
1.1.0 1,003 3/7/2017
1.0.0 727 11/29/2016

## Features
- Add support of the new .NET 6 `CallerArgumentExpression` attribute
- Add Check.NotEmpty(*Guid*)
- Add Check.True(*Func>bool<*)
- Add Check.PositiveOrNull()
- Add Check.NegativeOrNull()
- Add Check.NotNegativeOrNull()

## Breaking changes
- Supports .NET 6 and .NET Standard 2.0 only
- Remove Check.Zero()
- Rename Check.NotEmpty(IEnumerable) in Check.NotNullOrEmpty(IEnumerable)