Decode.Extensions 3.0.0

dotnet add package Decode.Extensions --version 3.0.0
                    
NuGet\Install-Package Decode.Extensions -Version 3.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="Decode.Extensions" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Decode.Extensions" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Decode.Extensions" />
                    
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 Decode.Extensions --version 3.0.0
                    
#r "nuget: Decode.Extensions, 3.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 Decode.Extensions@3.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=Decode.Extensions&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Decode.Extensions&version=3.0.0
                    
Install as a Cake Tool

Decode.Extensions

General purpose extension methods for .NET basic types, designed to simplify common operations like type conversion, enum parsing, and validation.

📦 Installation

dotnet add package Decode.Extensions

🛠️ Usage

1. Object Extensions (Type Conversion)

The library provides a suite of methods to handle type conversion safely and concisely.

Basic Conversion (To<T>)

Converts an object to a target type.

string input = "100";
int value = input.To<int>();

Culture (changed in 2.0.0). Conversion always uses InvariantCulture. These helpers map values coming from databases, API payloads and configuration, which are culture-neutral, so honouring the machine's regional settings made the same input produce different results on different hosts: under pt-BR, "1.5".To<decimal>() previously returned 15. It now returns 1.5 everywhere.

Types that do not implement IConvertible are handled explicitly, so they work like any other:

Guid id = "6f9619ff-8b86-d011-b42d-00c04fc964ff".To<Guid>();
DateTimeOffset when = "2026-01-15T10:30:00+00:00".To<DateTimeOffset>();
TimeSpan window = "01:30:00".To<TimeSpan>();

Before 2.0.0 these threw InvalidCastException — and the ToOrDefault/ToOrNull variants swallowed it, silently handing back Guid.Empty instead of the parsed value.

Safe Conversion with Default (ToOrDefault<T>)

Returns the default value of T if the input is null or conversion fails.

string? invalidInput = "abc";
int value = invalidInput.ToOrDefault<int>(); // returns 0
Safe Conversion to Nullable (ToOrNull<T>)

Returns null if the input is null or conversion fails (for value types).

string? input = "not a number";
int? value = input.ToOrNull<int>(); // returns null

2. Enum Extensions

Parse to Enum (ToEnum<T>)
string statusStr = "Active";
UserStatus status = statusStr.ToEnum<UserStatus>();
Safe Parse to Enum (ToEnumOrNull<T>)
string? invalidStatus = "UnknownValue";
UserStatus? status = invalidStatus.ToEnumOrNull<UserStatus>(); // returns null

3. Validation

Check if Parseable (IsParseableTo<T>)

Returns true if the conversion is possible without throwing an exception.

object rawValue = "123";
if (rawValue.IsParseableTo<int>())
{
    // safe to convert
}

📄 License

MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

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
3.0.0 84 9/14/2026
2.1.0 90 9/13/2026
2.0.2 85 9/13/2026
2.0.1 83 9/13/2026
2.0.0 116 7/28/2026
1.0.3 127 6/19/2026
1.0.2 124 5/26/2026
1.0.1 116 4/26/2026