Skip to content

C# Preconditions inspired by Google Guava Preconditions.

License

Notifications You must be signed in to change notification settings

stackify/Preconditions

 
 

Repository files navigation

Preconditions AppVeyor build status NuGet NuGet

This project is inspired by Google Guava Preconditions.

What is Preconditions ?

Preconditions provide convenience static methods that help to 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 returns the tested value on success allowing to check and call a method at the same time.

On failure its methods always throw an ArgumentException.

How to use it ?

Because Preconditions is only one code file, you can either copy paste the Check.cs class or include the Nuget package to your project : https://www.nuget.org/packages/Preconditions.NET

PM> Install-Package Preconditions.NET

A signed version is also available: https://www.nuget.org/packages/Preconditions.NET.StrongName

PM> Install-Package Preconditions.NET.StrongName

Example

public class Employee : Person
{
    public string Id { get; }

    public Employee(string name, string id) : base(Check.NotNullOrEmpty(name, nameof(name)))
    {
        Id = Check.NullableButNotEmpty(id, nameof(id));
    }
}

The Check list

  • Check.NotNullOrEmpty(string)
  • Check.NullableButNotEmpty (string)
  • Check.NotNull(object)
  • Check.NotEmpty(IEnumerable)
  • Check.HasNoNulls(IEnumerable)
  • Check.FileExists()
  • Check.DirectoryExists()
  • Check.Negative()
  • Check.Zero()
  • Check.Positive()

Feedback and issues

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

About

C# Preconditions inspired by Google Guava Preconditions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%