Nogic.ThrowHelperExtensions 1.0.0

dotnet add package Nogic.ThrowHelperExtensions --version 1.0.0
                    
NuGet\Install-Package Nogic.ThrowHelperExtensions -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="Nogic.ThrowHelperExtensions" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nogic.ThrowHelperExtensions" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Nogic.ThrowHelperExtensions">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nogic.ThrowHelperExtensions --version 1.0.0
                    
#r "nuget: Nogic.ThrowHelperExtensions, 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.
#:package Nogic.ThrowHelperExtensions@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nogic.ThrowHelperExtensions&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Nogic.ThrowHelperExtensions&version=1.0.0
                    
Install as a Cake Tool

ThrowHelperExtensions

GitHub release (latest by date) .NET CI codecov CodeFactor License

Use ThrowHelper methods (ex. ArgumentException.ThrowIfNull) on your elderly .NET project

Usage

1. Set LangVersion to C# 14

This package uses Extension members feature on C# 14.

Please Add below on your .csproj or Directory.Build.props.

<PropertyGroup>
  <LangVersion>14</LangVersion>
</PropertyGroup>

2. Add Package

dotnet add package Nogic.ThrowHelperExtensions

3. Use throw helpers

namespace Samples;

public class Sample
{
    public string Value1 { get; }

    public Sample(string value1)
    {
        // On .NET 6.0 or higher, it calls `ArgumentNullException.ThrowIfNull` directly.
        // On others, it calls `System.ExceptionPolyfills.ThrowIfNull` polyfill via extension members.
        ArgumentNullException.ThrowIfNull(value1);

        this.Value1 = value1;
    }
}

API

[^1]: INumberBase<T> is not available in .NET 6.0 or below (including .NET Standard 2.0). So, this method only has overloads for the built-in numeric types. (byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, nint, nuint, char)

Limitations

  • Except for ArgumentNullException.ThrowIfNull, exception messages are not localized.
  • Unlike .NET 8.0 and 9.0, these methods do not have IEquatable<T> constraint on type parameter T. (based on .NET 10.0+ source code)
    • ArgumentOutOfRangeException.ThrowIfEqual<T>(T, T, string?)
    • ArgumentOutOfRangeException.ThrowIfNotEqual<T>(T, T, string?)
  • The generated class System.ExceptionPolyfills is internal.

Options

ThrowHelperExtensionsGenerateAttributes

By default, this generator will generate necessary attribute types. (ex. DoesNotReturnAttribute) If you want to disable attribute generation (for example, use other generator like PolySharp), you can set the following MSBuild property in your project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <ThrowHelperExtensionsGenerateAttributes>false</ThrowHelperExtensionsGenerateAttributes>
  </PropertyGroup>
</Project>
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Nogic.ThrowHelperExtensions:

Package Downloads
WithOtherGenerator

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 376 11/13/2025
0.3.0 326 10/23/2025
0.2.0 170 10/21/2025
0.1.0 113 5/24/2025