Pozitron.Convert 2.0.0

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

A lightweight .NET library providing safe, null-tolerant conversion extension methods for string and object types. Conversion failures never throw — a default or null value is returned instead.

Installation

dotnet add package Pozitron.Convert

Target Frameworks

Framework Notes
.NET Standard 2.0 String extensions support a fixed set of types
.NET 10.0 String extensions support any IParsable<T> type

Usage

Converting strings

// Returns the default(T) when conversion fails or input is null
int count = "42".To<int>();
decimal price = "1.99".To<decimal>();

// Returns a custom default when conversion fails or input is null
int count = "abc".To<int>(defaultValue: -1);

// Returns null when conversion fails or input is null
int? count = "abc".ToNullable<int>();
decimal? price = "1.99".ToNullable<decimal>();

On .NET 10, To<T> and ToNullable<T> accept any type implementing IParsable<T>, such as Guid, DateOnly, TimeOnly, DateTimeOffset, and all numeric types.

var id = "d3b07384-d9a0-4f3a-8d1b-2e4f6c8a1b3d".To<Guid>();
var date = "2024-01-15".To<DateOnly>();

Converting objects

Particularly useful in desktop/WinForms applications where UI controls return object values.

double qty = controlValue.EditValue.To<double>();
int id = controlValue2.EditValue.To<int>(defaultValue: 0);
string? name = controlValue3.EditValue.To<string>();
int? nullableId = controlValue4.EditValue.ToNullable<int>();

Culture-aware conversion

Both string and object extension methods accept an optional IFormatProvider.

var price = "1,99".To<decimal>(provider: new CultureInfo("de-DE"));
var date = "01.01.2024".To<DateTime>(provider: new CultureInfo("de-DE"));

API

String Extensions

Extension methods on string?.

TFM Method Returns
(.NET 10) T? To<T>(this string? value, T? defaultValue = default, IFormatProvider? provider = null) where T : IParsable<T> Parsed value, or defaultValue on failure/null
(.NET 10) T? ToNullable<T>(this string? value, IFormatProvider? provider = null) where T : struct, IParsable<T> Parsed value, or null on failure/null
(.NET Standard 2.0) T To<T>(this string? value, T defaultValue = default) where T : struct Parsed value, or defaultValue on failure/null
(.NET Standard 2.0) T? ToNullable<T>(this string? value) where T : struct Parsed value, or null on failure/null

On .NET Standard 2.0, string extensions support: int, decimal, double, float, DateTime, bool.

Object Extensions

Extension methods on object?. Uses System.Convert.ChangeType internally and supports any IConvertible type, including string.

Method Returns
T? To<T>(this object? value, T? defaultValue = default, IFormatProvider? provider = null) where T : IConvertible Converted value, or defaultValue on failure/null
T? ToNullable<T>(this object? value, IFormatProvider? provider = null) where T : struct, IConvertible Converted value, or null on failure/null

The To<T> methods are annotated with [NotNullIfNotNull(nameof(defaultValue))] for accurate nullable static analysis on .NET 10.

Give a Star! ⭐

If you find this library useful, please give it a star. Thanks!

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.  net9.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  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 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.
  • .NETStandard 2.0

    • No dependencies.
  • net10.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
2.0.0 90 2/24/2026

Refer to Releases page for details.
     https://github.com/fiseni/Pozitron.Convert/releases