Decode.Extensions
3.0.0
dotnet add package Decode.Extensions --version 3.0.0
NuGet\Install-Package Decode.Extensions -Version 3.0.0
<PackageReference Include="Decode.Extensions" Version="3.0.0" />
<PackageVersion Include="Decode.Extensions" Version="3.0.0" />
<PackageReference Include="Decode.Extensions" />
paket add Decode.Extensions --version 3.0.0
#r "nuget: Decode.Extensions, 3.0.0"
#:package Decode.Extensions@3.0.0
#addin nuget:?package=Decode.Extensions&version=3.0.0
#tool nuget:?package=Decode.Extensions&version=3.0.0
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: underpt-BR,"1.5".To<decimal>()previously returned15. It now returns1.5everywhere.
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 | Versions 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. |
-
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.