Getting started
editInstall the package
Add the Meziantou.Framework.Versioning package to your project:
dotnet package add Meziantou.Framework.VersioningThe examples of this documentation omit the using directives: they need using Meziantou.Framework.Versioning;. They check their results with assertions such as Assert.Equal, which show what each call returns.
Parse a version
// Parse a semantic version string
var version = SemanticVersion.Parse("1.2.3-alpha.1+build");
Assert.Equal(1, version.Major);
Assert.Equal(2, version.Minor);
Assert.Equal(3, version.Patch);
Assert.True(version.IsPrerelease);
Assert.Equal(["alpha", "1"], version.PrereleaseLabels);
Assert.True(version.HasMetadata);
Assert.Equal(["build"], version.Metadata);
// Create and compare versions (prerelease versions have lower precedence)
var v1 = new SemanticVersion(1, 0, 0, "alpha");
var v2 = new SemanticVersion(1, 0, 0);
Assert.True(v1 < v2);
// Get the next version
Assert.Equal("1.0.1", v2.NextPatchVersion().ToString());
Assert.Equal("1.1.0", v2.NextMinorVersion().ToString());
Assert.Equal("2.0.0", v2.NextMajorVersion().ToString());- Major, Minor and Patch are the three numbers.
- PrereleaseLabels holds the dot-separated identifiers after
-, and IsPrerelease tells whether there is any. - Metadata holds the dot-separated identifiers after
+, and HasMetadata tells whether there is any.
Validation and formatting
Parse and TryParse accept the Semantic Versioning 2.0.0 syntax, and nothing else:
// A leading "v" is accepted, and not written back
var version = SemanticVersion.Parse("v2.1.0-rc.1+build.42");
Assert.Equal("2.1.0-rc.1+build.42", version.ToString());
// Versions that do not follow Semantic Versioning 2.0.0 are rejected
Assert.False(SemanticVersion.TryParse("1.0", out _)); // Three numbers are required
Assert.False(SemanticVersion.TryParse("1.02.0", out _)); // No leading zero
Assert.False(SemanticVersion.TryParse("1.0.0-beta.01", out _));
Assert.Throws<ArgumentException>(() => SemanticVersion.Parse("1.0.0.0"));
// Create a version from its parts
var prerelease = new SemanticVersion(1, 4, 0, prereleaseLabel: "beta.2", metadata: "sha.5114f85");
Assert.Equal("1.4.0-beta.2+sha.5114f85", prerelease.ToString());
Assert.Equal(["beta", "2"], prerelease.PrereleaseLabels);
Assert.Equal(["sha", "5114f85"], prerelease.Metadata);- A
vorVprefix is accepted, as in Git tags such asv1.2.3.ToStringnever writes it. - The three numbers are required, and numbers have no leading zero. Prerelease and metadata identifiers are made of ASCII letters, digits and
-, and are not empty. - Parse throws an
ArgumentExceptionfor an invalid version; TryParse returnsfalse. Both have aReadOnlySpan<char>overload, and SemanticVersion implementsIParsable<SemanticVersion>andISpanParsable<SemanticVersion>. - The constructors validate the labels too and throw an
ArgumentExceptionfor an invalid one, and a negative number throws anArgumentOutOfRangeException. One overload takes the prerelease labels and the metadata as dot-separated strings, another as lists of identifiers. ToStringwrites the canonical form. The only formats arenull, an empty string and"G"; the result does not depend on the culture.
Next steps
- Comparing versions: sort versions and compute the next one.
- Version ranges: check a version against a NuGet or npm range.