Skip to content

feO2x/Light.GuardClauses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,244 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Light.GuardClauses

A lightweight .NET library for expressive guard clauses.

License NuGet Source Code Documentation Changelog

Light.GuardClauses replaces repetitive parameter checks with expressive extension methods:

public class Foo
{
    private readonly IBar _bar;

    public Foo(IBar? bar)
    {
        _bar = bar.MustNotBeNull();
    }
}

The guard returns the validated value, so validation and assignment can stay together. Throwing guards start with Must; Boolean checks such as IsNullOrEmpty, IsValidEnumValue, and IsFileExtension can be used in branching logic.

public void SetMovieRating(Guid movieId, int numberOfStars)
{
    movieId.MustNotBeEmpty();
    numberOfStars.MustBeIn(Range.InclusiveBetween(0, 5));

    var movie = _movieRepo.GetById(movieId);
    movie.AddRating(numberOfStars);
}

See the documentation index, assertion overview, and usage guide for more.

Supported platforms

The NuGet package contains these assets:

Target Purpose
.NET Standard 2.0 Broad compatibility, including implementations of .NET Standard 2.0
.NET Standard 2.1 Implementations of .NET Standard 2.1
.NET 10 The current .NET asset, including modern generic-math, span, and memory overloads and Native AOT compatibility

Caller argument expressions are understood by C# 10 and newer compilers and automatically capture expressions such as bar for the exception parameter name. This is independent of the target framework. With an older C# compiler, pass the name explicitly:

bar.MustNotBeNull(nameof(bar));

Installation

Light.GuardClauses is available from NuGet.

  • .NET CLI: dotnet add package Light.GuardClauses
  • Package Manager Console: Install-Package Light.GuardClauses
  • Project file: <PackageReference Include="Light.GuardClauses" />

To embed the library without a DLL dependency, use the committed .NET Standard 2.0 single-file distribution or create a tailored file with the source exporter.

Design and quality

The library supports nullable reference types, .NET code-analysis attributes, JetBrains contract annotations, and Native AOT. Its functional behavior is covered by the test suite, and performance-sensitive assertions can be measured with the current benchmark project.

For the design history behind guard clauses and design by contract, see Guard clause background.

Light Libraries logo

About

A lightweight .NET library for expressive Guard Clauses.

Topics

Resources

License

Contributing

Stars

93 stars

Watchers

3 watching

Forks

Contributors

Languages