Heleonix.Guard 1.0.0

.NET Standard 1.6 .NET Framework 4.5
Install-Package Heleonix.Guard -Version 1.0.0
dotnet add package Heleonix.Guard --version 1.0.0
<PackageReference Include="Heleonix.Guard" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Heleonix.Guard --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Heleonix.Guard, 1.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 Heleonix.Guard as a Cake Addin
#addin nuget:?package=Heleonix.Guard&version=1.0.0

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

Heleonix.Guard

Provides performant guard functionality for methods to throw exceptions

Install

https://www.nuget.org/packages/Heleonix.Guard

Example

using Heleonix.Guard;
using Heleonix.Extensions;
using static Heleonix.Guard.Host;

public static class Test
{
    public static void Main()
    {
        MyMethod(null);
    }

    public static void MyMethod(string param)
    {
        // C# 7.2+: Non-Trailing named arguments
        Throw.ArgumentNullException(when: param.IsNull(), nameof(param));

        // OR

        // Prior to C# 7.2: You can use a helper method 'When'
        Throw.ArgumentNullException(When(param.IsNull()), nameof(param));

        // OR
        Throw.ArgumentNullException(param == null, nameof(param));

        // OR
        Throw.ArgumentNullException(When(param == null), nameof(param));
    }
}

The Heleonix.Extensions provides many useful predicative extensions to build assertions, like IsNull(), IsNullOrEmptyOrWhiteSpace(), IsLessThan() etc. These extensions do not throw exceptions, so they will not overlap an exception to be really thrown. See Heleonix.Extensions for more details.

The Host.Throw property returns a singleton instance of the Heleonix.Guard.ExceptionRaiser class, which provides methods to throw many existing exceptions, like ArgumentNullException, FileLoadException etc. See more via VisualStudio intellisense.

The ExceptionRaiser returned by the Host.Throw is the singleton instance, so there is no creations of new objects every time, as it is usually implemented in fluent interfaces. Besides, it also provides ability to implement custom exception raisers via extension methods:

using Heleonix.Guard;

public static class ExceptionRaiserExtensions
{
#pragma warning disable CC0057 // Unused parameters
    public static void MyCustomException(this ExceptionRaiser raiser, bool when, int param1, string param2, object paramN, string message = null, Exception innerException = null)
    {
        if (when)
        {
            throw new MyCustomException(param1, param2, paramN, message, innerException);
        }
    }
#pragma warning restore CC0057 // Unused parameters
}

and then use it, for example, as below:

Throw.MyCustomException(when: param1.IsZero() || param2.IsNullOrEmpty() || paramN.IsNull(), "some message");
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.0 2,509 6/22/2018

Split from the Heleonix.Utilities