Git Product home page Git Product logo

preconditions's Introduction

Preconditions.NET AppVeyor build status NuGet

Preconditions.NET is a personal, free-time project with no funding. If you use Evolve in your daily work and feel that it makes your life easier, consider supporting its development via GitHub Sponsors ❤️ and by adding a star to this repository ⭐

Introduction

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 v2.0.0 is a complete rewrite that takes advantage of nullable types, the NotNull attribute and 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

Example

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)

Feedback and issues

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

preconditions's People

Contributors

lecaillon avatar strvmarv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.