Alyio.Extensions 2.6.0

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

Alyio.Extensions

Build Status

Alyio.Extensions is a .NET library that provides a comprehensive set of robust and high-performance extension methods for seamless type conversions across various base data types. It aims to simplify common conversion tasks, reduce boilerplate code, and enhance code readability and maintainability.

Features

  • Object Extensions: Offers flexible methods to convert any object to various primitive types (bool, int, long, double, decimal, DateTime, DateTimeOffset, DateOnly, enum) with graceful handling of nulls and conversion failures.

    • ToBoolean(): Converts an object to a boolean.
    • ToInt32(), ToInt64(): Converts an object to a 32-bit or 64-bit integer.
    • ToDouble(), ToDecimal(): Converts an object to a double or decimal.
    • ToDateTime(), ToDateTimeOffset(), ToDateOnly(): Converts an object to DateTime, DateTimeOffset, or DateOnly.
    • ToEnum<T>(): Converts an object to a specified enumeration type.
    • ToStringOrEmpty(): Safely converts an object to its string representation, returning string.Empty for nulls.
  • String Extensions: Provides utilities for converting string representations to various data types, handling different formats and cultural conventions.

    • ToBoolean(): Converts a string to a boolean, supporting "true"/"false" and numeric strings ("0" for false, non-zero for true).
    • ToInt32(), ToInt64(), ToDouble(), ToDecimal(): Converts string representations of numbers to their respective types.
    • ToDateTime(), ToDateTimeOffset(), ToDateOnly(): Parses strings into DateTime, DateTimeOffset, or DateOnly objects.
    • ToEnum<T>(): Converts a string to a specified enumeration type.
  • DateTime Extensions: Simplifies common DateTime and DateTimeOffset operations, including Unix timestamp conversions and specific formatting.

    • ToDateInt32(): Converts DateTime to an integer in yyyyMMdd format.
    • ToyyyyMMddHHmmss(): Formats DateTime to "yyyy-MM-dd HH:mm:ss" string.
    • ToUnix(): Converts DateTime or DateTimeOffset to a Unix timestamp (seconds since epoch).
    • ToDateTime(): Converts Unix timestamp (long or double) or DateTimeOffset to DateTime.
    • ToDateTimeOffset(): Converts DateTime or Unix timestamp (long) to DateTimeOffset.
  • Byte Extensions: Offers utilities for converting byte arrays to hexadecimal string representations.

    • ToHex(): Converts a byte array to its hexadecimal string representation.

Installation

You can install the package via NuGet:

dotnet add package Alyio.Extensions

Or using the NuGet Package Manager:

Install-Package Alyio.Extensions

Usage

using Alyio.Extensions;
using System;
using System.Globalization;

// --- String Extensions ---

// String to number conversion - returns null if conversion fails
string validNumber = "123";
int? convertedNumber = validNumber.ToInt32(); // Returns 123

string invalidNumber = "abc";
int? failedNumber = invalidNumber.ToInt32(); // Returns null

// String to boolean conversion
"true".ToBoolean(); // Returns true
"false".ToBoolean(); // Returns false
"1".ToBoolean(); // Returns true (numeric string)
"0".ToBoolean(); // Returns false (numeric string)
"yes".ToBoolean(); // Returns false (not "true" or "false" or numeric)

// String to DateTimeOffset
string dateStringWithOffset = "2024-03-20T10:30:00+01:00";
DateTimeOffset? dto1 = dateStringWithOffset.ToDateTimeOffset(); // Returns DateTimeOffset with +01:00 offset

string dateStringWithoutOffset = "2024-03-20 10:30:00";
DateTimeOffset? dto2 = dateStringWithoutOffset.ToDateTimeOffset(); // Returns DateTimeOffset with local system's offset

// String to DateOnly (for .NET 6.0+)
#if NET6_0_OR_GREATER
string dateOnlyString = "2024-03-20";
DateOnly? dateOnly = dateOnlyString.ToDateOnly(); // Returns DateOnly(2024, 3, 20)
#endif

// --- Object Extensions ---

// Object to number conversion - returns null if conversion fails
object validValue = "42";
int? validResult = validValue.ToInt32(); // Returns 42

object invalidValue = "not a number";
int? invalidResult = invalidValue.ToInt32(); // Returns null

// Object to DateTimeOffset
object dateTimeObject = new DateTime(2024, 3, 20, 10, 30, 0, DateTimeKind.Utc);
DateTimeOffset? dtoFromObject = dateTimeObject.ToDateTimeOffset(); // Returns DateTimeOffset with UTC offset

object unixSecondsObject = 1710921000L; // Unix timestamp for 2024-03-20 10:30:00 UTC
DateTimeOffset? dtoFromUnix = unixSecondsObject.ToDateTimeOffset(); // Returns DateTimeOffset for 2024-03-20 10:30:00 UTC

// Object to Enum
public enum Status { None, Active, Inactive }
object statusString = "Active";
Status? enumStatus = statusString.ToEnum<Status>(); // Returns Status.Active

// --- DateTime Extensions ---

// DateTime to Unix timestamp
DateTime now = DateTime.UtcNow;
long unixTimestamp = now.ToUnix(); // Returns seconds since epoch

// Unix timestamp to DateTime
long someUnixTimestamp = 1678886400; // March 15, 2023 12:00:00 AM UTC
DateTime? fromUnix = someUnixTimestamp.ToDateTime(); // Returns DateTime for March 15, 2023 12:00:00 AM UTC

// DateTime to DateTimeOffset
DateTime localDt = new DateTime(2024, 3, 20, 10, 30, 0, DateTimeKind.Local);
DateTimeOffset localDto = localDt.ToDateTimeOffset(); // Uses local system's offset

// DateTimeOffset to Unix timestamp
DateTimeOffset currentDto = DateTimeOffset.UtcNow;
long dtoUnixTimestamp = currentDto.ToUnix();

// --- Byte Extensions ---

// Byte array to Hex string
byte[] data = { 0xDE, 0xAD, 0xBE, 0xEF };
string hexString = data.ToHex(); // Returns "DEADBEEF"

// Handling conversion results with null-coalescing operator
string? input = "123";
int result = input.ToInt32() ?? -1; // Returns 123 if conversion succeeds, -1 if it fails

Supported Frameworks

  • .NET Standard 2.0
  • .NET 6.0
  • .NET 8.0
  • .NET 9.0

Development

To build the project, navigate to the root directory of the repository and run:

dotnet build

To run the tests, execute:

dotnet test

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Jon X

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 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. 
.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.
  • net6.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
2.6.0 325 11/13/2025
2.5.1 326 9/26/2025
2.5.1-preview.10 114 9/26/2025
2.3.2 355 6/9/2025
2.3.1 305 3/15/2024
2.3.0 487 10/20/2023
2.1.0 651 5/28/2022
2.0.0 3,056 8/22/2017
1.1.2 1,921 8/4/2017
1.1.1 1,774 6/9/2017
1.1.0 1,698 5/7/2017
1.0.2 1,695 4/5/2017
1.0.1 1,802 12/22/2016
1.0.0 1,724 12/19/2016