SemanticVersion Class

Implements Semantic Version 2.0.0.

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

NameDescription
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.cs

Creates a new semantic version with the specified major, minor, and patch numbers.

public SemanticVersion(int major, int minor, int patch)
Parameters
major int
minor int
patch int
Exceptions
ArgumentOutOfRangeException
major, minor or patch is negative.

SemanticVersion(int, int, int, IEnumerable<string>?, IEnumerable<string>?)

SemanticVersion.cs

Creates 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
major int
minor int
patch int
prereleaseLabel IEnumerable<string>?
metadata IEnumerable<string>?

SemanticVersion(int, int, int, string?)

SemanticVersion.cs

Creates a new semantic version with the specified major, minor, patch numbers and prerelease label.

public SemanticVersion(int major, int minor, int patch, string? prereleaseLabel)
Parameters
major int
minor int
patch int
prereleaseLabel string?

SemanticVersion(int, int, int, string?, string?)

SemanticVersion.cs

Creates a new semantic version with the specified major, minor, patch numbers, prerelease label, and metadata.

public SemanticVersion(int major, int minor, int patch, string? prereleaseLabel, string? metadata)
Parameters
major int
minor int
patch int
prereleaseLabel string?
metadata string?

Properties

NameDescription
HasMetadataGets a value indicating whether this version has build metadata.
IsPrereleaseGets a value indicating whether this version is a prerelease version.
MajorGets the major version number.
MetadataGets the build metadata labels.
MinorGets the minor version number.
PatchGets the patch version number.
PrereleaseLabelsGets the prerelease labels (e.g., alpha, beta, rc.1).

Gets a value indicating whether this version has build metadata.

public bool HasMetadata { get; }

Property value

bool

IsPrerelease

SemanticVersion.cs

Gets a value indicating whether this version is a prerelease version.

public bool IsPrerelease { get; }

Property value

bool

Gets the major version number.

public int Major { get; }

Property value

int

Gets the build metadata labels.

public IReadOnlyList<string> Metadata { get; }

Property value

IReadOnlyList<string>

Gets the minor version number.

public int Minor { get; }

Property value

int

Gets the patch version number.

public int Patch { get; }

Property value

int

PrereleaseLabels

SemanticVersion.cs

Gets the prerelease labels (e.g., alpha, beta, rc.1).

public IReadOnlyList<string> PrereleaseLabels { get; }

Property value

IReadOnlyList<string>

Methods

NameDescription
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.cs

Compares 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)
Parameters
other SemanticVersion?
Returns

int

CompareTo(object?)

SemanticVersion.cs

Compares the current version to a specified object and returns an integer that indicates their relative position in the sort order.

public int CompareTo(object? obj)
Parameters
obj object?
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
ArgumentException
obj is neither null nor a SemanticVersion.

Equals overloads

Equals(SemanticVersion?)

SemanticVersion.cs

Determines whether the specified semantic version is equal to the current version.

public bool Equals(SemanticVersion? other)
Parameters
other SemanticVersion?
Returns

bool

Equals(object?)

SemanticVersion.cs
public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

GetHashCode()

SemanticVersion.cs
public override int GetHashCode()

Returns

int

No documentation is available.

Parse overloads

Parse(ReadOnlySpan<char>)

SemanticVersion.cs

Converts the span representation of a semantic version to its SemanticVersion equivalent.

public static SemanticVersion Parse(ReadOnlySpan<char> versionString)
Parameters
versionString ReadOnlySpan<char>
Returns

SemanticVersion

Exceptions
ArgumentException
versionString is not a valid semantic version.

Parse(string)

SemanticVersion.cs

Converts the string representation of a semantic version to its SemanticVersion equivalent.

public static SemanticVersion Parse(string versionString)
Parameters
versionString string
Returns

SemanticVersion

Exceptions
ArgumentNullException
versionString is null.
ArgumentException
versionString is not a valid semantic version.

ToString overloads

public override string ToString()
Returns

string

No documentation is available.

ToString(string?, IFormatProvider?)

SemanticVersion.cs

Formats the semantic version as a string.

public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
format string?
The format to use. Only the general format (null, an empty string, or "G") is supported.
formatProvider IFormatProvider?
Ignored. A semantic version has no culture-sensitive representation.
Returns

string

Exceptions
FormatException
format is not a supported format.

TryParse overloads

TryParse(ReadOnlySpan<char>, SemanticVersion?)

SemanticVersion.cs

Tries 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)
Parameters
versionString ReadOnlySpan<char>
version SemanticVersion?
Returns

bool

TryParse(string?, SemanticVersion?)

SemanticVersion.cs

Tries 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)
Parameters
versionString string?
version SemanticVersion?
Returns

bool

Operators

NameDescription
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.cs

Determines whether two semantic versions are equal.

public static bool operator ==(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool

operator >(SemanticVersion?, SemanticVersion?)

SemanticVersion.cs

Determines whether one semantic version is greater than another.

public static bool operator >(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool

operator >=(SemanticVersion?, SemanticVersion?)

SemanticVersion.cs

Determines whether one semantic version is greater than or equal to another.

public static bool operator >=(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool

operator !=(SemanticVersion?, SemanticVersion?)

SemanticVersion.cs

Determines whether two semantic versions are not equal.

public static bool operator !=(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool

operator <(SemanticVersion?, SemanticVersion?)

SemanticVersion.cs

Determines whether one semantic version is less than another.

public static bool operator <(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool

operator <=(SemanticVersion?, SemanticVersion?)

SemanticVersion.cs

Determines whether one semantic version is less than or equal to another.

public static bool operator <=(SemanticVersion? left, SemanticVersion? right)

Parameters

left SemanticVersion?
right SemanticVersion?

Returns

bool