Create, parse, and manipulate semantic version numbers with the SemVersion
class and semantic
version ranges with the SemVersionRange
class. This library complies with v2.0.0 of the semantic
versioning spec from semver.org.
API docs for the most recent release are available online at semver-nuget.org.
You may have noticed that this repository has moved between accounts recently (from maxhauser to WalkerCodeRanger). This move reflects who has been the primary maintainer for the last five years. Nothing will change with how the project is managed or run. Look for the next major release, 3.0, to come out within the next month or two! (Sept. or Oct. 2024)
var version = SemVersion.Parse("1.1.0-rc.1+e471d15", SemVersionStyles.Strict);
var v1 = new SemVersion(1, 0);
var vNextRc = SemVersion.ParsedFrom(1, 1, 0, "rc.1");
if (version.ComparePrecedenceTo(vNextRc) == 0)
Console.WriteLine($"{version} has the same precedence as {vNextRc}");
if (version.CompareSortOrderTo(vNextRc) > 0)
Console.WriteLine($"{version} sorts after {vNextRc}");
Outputs:
1.1.0-rc.1+e471d15 has the same precedence as 1.1.0-rc.1
1.1.0-rc.1+e471d15 sorts after 1.1.0-rc.1
Console.WriteLine($"Current: {version}");
if (version.IsPrerelease)
{
Console.WriteLine($"Prerelease: {version.Prerelease}");
Console.WriteLine($"Next release version is: {version.WithoutPrereleaseOrMetadata()}");
}
Outputs:
Current: 1.1.0-rc.1+nightly.2345
Prerelease: rc.1
Next release version is: 1.1.0
var range = SemVersionRange.Parse("^1.0.0");
var prereleaseRange = SemVersionRange.ParseNpm("^1.0.0", includeAllPrerelease: true);
Console.WriteLine($"Range: {range}");
Console.WriteLine($"Prerelease range: {prereleaseRange}");
Console.WriteLine($"Range includes version {version}: {range.Contains(version)}");
Console.WriteLine($"Prerelease range includes version {version}: {prereleaseRange.Contains(version)}");
// Alternative: another way to call SemVersionRange.Contains(version)
version.Satisfies(range);
// Alternative: slower because it parses the range on every call
version.SatisfiesNpm("^1.0.0", includeAllPrerelease: true)
Outputs:
Range: 1.*
Prerelease range: *-* 1.*
Range includes version 1.1.0-rc.1+e471d15: False
Prerelease range includes version 1.1.0-rc.1+e471d15: True