Heleonix.Guard 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Heleonix.Guard --version 1.0.0
NuGet\Install-Package Heleonix.Guard -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: Heleonix.Guard, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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
2.0.0 376 10/7/2022
1.0.0 2,908 6/22/2018

Split from the Heleonix.Utilities