StringVersion 1.0.2

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

logo

GitHub license NuGet Actions

StringVersion

A lightweight, high-performance .NET library for parsing and comparing string-based versions, supporting multiple comparison strategies (including SemVer). Features a Span-based tokenizer, custom strategies, and is compatible with a wide range of .NET targets.

Features

  • Parse and compare version strings (e.g., 1.2.3, 1.2.3-beta, 2023.01.01)
  • Semantic Versioning (SemVer) comparison out of the box
  • Customizable comparison strategies via IVersionCompareStrategy
  • Span-based tokenizer for minimal allocations
  • Operator overloads for intuitive comparisons (==, !=, >, <, etc.)
  • Supports .NET Standard 2.0/2.1, .NET Framework 4.6.2+, .NET 5+, .NET 6+, .NET 7+, .NET 8+, .NET 9, .NET 10

Installation

You can add the package via NuGet (when published):

dotnet add package StringVersion

Usage

Basic Construction & Parsing

using System.StringVersion;

// Construct directly
var v1 = new StringVersion("1.2.3-beta");

// Parse (throws on invalid input)
var v2 = StringVersion.Parse("1.2.3");

// TryParse (safe, returns bool)
if (StringVersion.TryParse("2.0.0-rc1", out var v3))
    Console.WriteLine($"Parsed: {v3}");

// ToString
Console.WriteLine(v1.ToString()); // Output: 1.2.3-beta

Comparison

// Operator overloads
if (v1 < v2)
    Console.WriteLine($"{v1} is less than {v2}");
if (v1 == v2)
    Console.WriteLine("Equal");

// CompareTo
int cmp = v1.CompareTo(v2); // -1, 0, 1

// Equals
bool eq = v1.Equals(v2);
Supported Operators
  • ==, !=
  • <, >, <=, >=
if (v1 >= v2) { /* ... */ }

Semantic Versioning (SemVer)

var semver1 = new StringVersion("1.2.3-alpha.1");
var semver2 = new StringVersion("1.2.3-alpha.2");
Console.WriteLine(semver1 < semver2); // True

Other Comparison Formats

new StringVersion("V1.2.3") > 1; // True
new StringVersion("V1.2.3") > 1L; // True
new StringVersion("V1.2.3") > 1.2d; // True
new StringVersion("V1.2.3") == "1.2.3"; // True
new StringVersion("V1.2.3") == (1, 2, 3); // True
new StringVersion("V1.2.3") == new Version(1, 2, 3); // True
new StringVersion("V1.2.3") == new StringVersion("1.2.3"); // True

Custom Comparison Strategy

Implement IVersionCompareStrategy to define your own comparison logic:

public class MyStrategy : IVersionCompareStrategy
{
    public int Compare(ReadOnlySpan<StringToken> x, ReadOnlySpan<StringToken> y)
    {
        // Custom comparison logic
        return x.Length.CompareTo(y.Length); // Example
    }
}

// Use custom strategy
var custom = new StringVersion("1.2.3", new MyStrategy());

Advanced Usage

  • Supports IComparable, IEquatable
  • Can be used as dictionary keys or in sets
  • Efficient, allocation-minimizing parsing (Span-based)
  • Handles versions with prefixes, suffixes, and various delimiters
// Sorting
var versions = new[] {
    new StringVersion("1.0.0"),
    new StringVersion("1.0.0-beta"),
    new StringVersion("2.0.0")
};
Array.Sort(versions);

// Dictionary usage
var dict = new Dictionary<StringVersion, string>();
dict[new StringVersion("1.0.0")] = "Release 1";

For more details and extension scenarios, see the source code and interface definitions.

Project Structure

  • src/System.StringVersion/ - Main library
  • src/System.StringVersion.Tests/ - xUnit test project
  • src/System.StringVersion.Benchmarks/ - BenchmarkDotNet performance benchmarks

Build & Test

Run tests:

dotnet test src/System.StringVersion.Tests

Run benchmarks:

dotnet run -c Release --project src/System.StringVersion.Benchmarks

Target Frameworks

  • .NET Standard 2.0, 2.1
  • .NET Framework 4.6.2, 4.7.2, 4.8, 4.8.1
  • .NET 5, 6, 7, 8, 9, 10

License

MIT

Repository

https://github.com/emako/StringVersion

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 is compatible.  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 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
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.

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
1.0.2 59 1/8/2026
1.0.1 64 1/7/2026
1.0.0 57 1/7/2026