SemanticVersion Class
Implements Semantic Version 2.0.0.
public sealed class SemanticVersion : IComparable, IComparable<SemanticVersion>, IEquatable<SemanticVersion>, IFormattable, IParsable<SemanticVersion>, ISpanParsable<SemanticVersion>Example
// 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());- Implements
Constructors
| Name | Description |
|---|---|
| SemanticVersion(int, int, int) | Creates a new semantic version with the specified major, minor, and patch numbers. |
| SemanticVersion(int, int, int, IEnumerable<string>?, IEnumerable<string>?) | Creates a new semantic version with the specified major, minor, patch numbers, prerelease labels, and metadata. |
| SemanticVersion(int, int, int, string?) | Creates a new semantic version with the specified major, minor, patch numbers and prerelease label. |
| SemanticVersion(int, int, int, string?, string?) | Creates a new semantic version with the specified major, minor, patch numbers, prerelease label, and metadata. |
SemanticVersion overloads
SemanticVersion(int, int, int)
SemanticVersion.csSemanticVersion(int, int, int, IEnumerable<string>?, IEnumerable<string>?)
SemanticVersion.csCreates a new semantic version with the specified major, minor, patch numbers, prerelease labels, and metadata.
public SemanticVersion(int major, int minor, int patch, IEnumerable<string>? prereleaseLabel, IEnumerable<string>? metadata)Parameters
majorintminorintpatchintprereleaseLabelIEnumerable<string>?metadataIEnumerable<string>?
SemanticVersion(int, int, int, string?)
SemanticVersion.csSemanticVersion(int, int, int, string?, string?)
SemanticVersion.csProperties
| Name | Description |
|---|---|
| HasMetadata | Gets a value indicating whether this version has build metadata. |
| IsPrerelease | Gets a value indicating whether this version is a prerelease version. |
| Major | Gets the major version number. |
| Metadata | Gets the build metadata labels. |
| Minor | Gets the minor version number. |
| Patch | Gets the patch version number. |
| PrereleaseLabels | Gets the prerelease labels (e.g., alpha, beta, rc.1). |
HasMetadata
SemanticVersion.csIsPrerelease
SemanticVersion.csMajor
SemanticVersion.csMetadata
SemanticVersion.csMinor
SemanticVersion.csPatch
SemanticVersion.csPrereleaseLabels
SemanticVersion.csGets the prerelease labels (e.g., alpha, beta, rc.1).
public IReadOnlyList<string> PrereleaseLabels { get; }Property value
Methods
| Name | Description |
|---|---|
| CompareTo(SemanticVersion?) | Compares the current version to a specified semantic version and returns an integer that indicates their relative position in the sort order. |
| CompareTo(object?) | Compares the current version to a specified object and returns an integer that indicates their relative position in the sort order. |
| Equals(SemanticVersion?) | Determines whether the specified semantic version is equal to the current version. |
| Equals(object?) | |
| GetHashCode() | |
| Parse(ReadOnlySpan<char>) | Converts the span representation of a semantic version to its SemanticVersion equivalent. |
| Parse(string) | Converts the string representation of a semantic version to its SemanticVersion equivalent. |
| ToString() | |
| ToString(string?, IFormatProvider?) | Formats the semantic version as a string. |
| TryParse(ReadOnlySpan<char>, SemanticVersion?) | Tries to convert the span representation of a semantic version to its SemanticVersion equivalent. A return value indicates whether the conversion succeeded. |
| TryParse(string?, SemanticVersion?) | Tries to convert the string representation of a semantic version to its SemanticVersion equivalent. A return value indicates whether the conversion succeeded. |
CompareTo overloads
CompareTo(SemanticVersion?)
SemanticVersion.csCompares the current version to a specified semantic version and returns an integer that indicates their relative position in the sort order.
public int CompareTo(SemanticVersion? other)CompareTo(object?)
SemanticVersion.csCompares the current version to a specified object and returns an integer that indicates their relative position in the sort order.
Parameters
objobject?
Returns
int A positive value if obj is null, as every instance is greater than null; otherwise the result of comparing the two versions.
Exceptions
ArgumentExceptionobjis neithernullnor aSemanticVersion.
Equals overloads
Equals(SemanticVersion?)
SemanticVersion.csDetermines whether the specified semantic version is equal to the current version.
public bool Equals(SemanticVersion? other)Equals(object?)
SemanticVersion.csGetHashCode()
SemanticVersion.csParse overloads
Parse(ReadOnlySpan<char>)
SemanticVersion.csConverts the span representation of a semantic version to its SemanticVersion equivalent.
public static SemanticVersion Parse(ReadOnlySpan<char> versionString)Parameters
versionStringReadOnlySpan<char>
Returns
Exceptions
ArgumentExceptionversionStringis not a valid semantic version.
Parse(string)
SemanticVersion.csConverts the string representation of a semantic version to its SemanticVersion equivalent.
public static SemanticVersion Parse(string versionString)Parameters
versionStringstring
Returns
Exceptions
ArgumentNullExceptionversionStringisnull.ArgumentExceptionversionStringis not a valid semantic version.
ToString overloads
ToString()
SemanticVersion.csToString(string?, IFormatProvider?)
SemanticVersion.csFormats the semantic version as a string.
public string ToString(string? format, IFormatProvider? formatProvider)Parameters
formatstring?- The format to use. Only the general format (
null, an empty string, or"G") is supported. formatProviderIFormatProvider?- Ignored. A semantic version has no culture-sensitive representation.
Returns
Exceptions
FormatExceptionformatis not a supported format.
TryParse overloads
TryParse(ReadOnlySpan<char>, SemanticVersion?)
SemanticVersion.csTries to convert the span representation of a semantic version to its SemanticVersion equivalent. A return value indicates whether the conversion succeeded.
public static bool TryParse(ReadOnlySpan<char> versionString, out SemanticVersion? version)TryParse(string?, SemanticVersion?)
SemanticVersion.csTries to convert the string representation of a semantic version to its SemanticVersion equivalent. A return value indicates whether the conversion succeeded.
public static bool TryParse(string? versionString, out SemanticVersion? version)Operators
| Name | Description |
|---|---|
| operator ==(SemanticVersion?, SemanticVersion?) | Determines whether two semantic versions are equal. |
| operator >(SemanticVersion?, SemanticVersion?) | Determines whether one semantic version is greater than another. |
| operator >=(SemanticVersion?, SemanticVersion?) | Determines whether one semantic version is greater than or equal to another. |
| operator !=(SemanticVersion?, SemanticVersion?) | Determines whether two semantic versions are not equal. |
| operator <(SemanticVersion?, SemanticVersion?) | Determines whether one semantic version is less than another. |
| operator <=(SemanticVersion?, SemanticVersion?) | Determines whether one semantic version is less than or equal to another. |
operator ==(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether two semantic versions are equal.
public static bool operator ==(SemanticVersion? left, SemanticVersion? right)operator >(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether one semantic version is greater than another.
public static bool operator >(SemanticVersion? left, SemanticVersion? right)operator >=(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether one semantic version is greater than or equal to another.
public static bool operator >=(SemanticVersion? left, SemanticVersion? right)operator !=(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether two semantic versions are not equal.
public static bool operator !=(SemanticVersion? left, SemanticVersion? right)operator <(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether one semantic version is less than another.
public static bool operator <(SemanticVersion? left, SemanticVersion? right)operator <=(SemanticVersion?, SemanticVersion?)
SemanticVersion.csDetermines whether one semantic version is less than or equal to another.
public static bool operator <=(SemanticVersion? left, SemanticVersion? right)