Class SemVersion
A semantic version number. Conforms with v2.0.0 of semantic versioning
(
semver.org).
Inheritance
System.Object
SemVersion
Implements
System.IComparable
System.Runtime.Serialization.ISerializable
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
Assembly: Semver.dll
Syntax
public sealed class SemVersion : IComparable<SemVersion>, IComparable, IEquatable<SemVersion>, ISerializable
Constructors
SemVersion(Int32)
Constructs a new instance of the
SemVersion class.
Declaration
public SemVersion(int major)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
SemVersion(Int32, Int32)
Constructs a new instance of the
SemVersion class.
Declaration
public SemVersion(int major, int minor)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
SemVersion(Int32, Int32, Int32)
Constructs a new instance of the
SemVersion class.
Declaration
public SemVersion(int major, int minor, int patch)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
System.Int32 |
patch |
The patch version number. |
Constructs a new instance of the
SemVersion class.
Declaration
public SemVersion(int major, int minor = 0, int patch = 0, IEnumerable<PrereleaseIdentifier> prerelease = null, IEnumerable<MetadataIdentifier> metadata = null)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
System.Int32 |
patch |
The patch version number. |
System.Collections.Generic.IEnumerable<PrereleaseIdentifier> |
prerelease |
The prerelease identifiers. |
System.Collections.Generic.IEnumerable<MetadataIdentifier> |
metadata |
The build metadata identifiers. |
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
A major ,
minor , or patch version number is negative. |
System.ArgumentException |
A prerelease or metadata identifier has the default value. |
SemVersion(Int32, Int32, Int32, IEnumerable<String>, IEnumerable<String>)
Constructs a new instance of the
SemVersion class.
Declaration
public SemVersion(int major, int minor = 0, int patch = 0, IEnumerable<string> prerelease = null, IEnumerable<string> metadata = null)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
System.Int32 |
patch |
The patch version number. |
System.Collections.Generic.IEnumerable<System.String> |
prerelease |
The prerelease identifiers. |
System.Collections.Generic.IEnumerable<System.String> |
metadata |
The build metadata identifiers. |
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
A major ,
minor , or patch version number is negative. |
System.ArgumentNullException |
One of the prerelease or metadata identifiers is null. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier. Or, a metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
SemVersion(Int32, Int32, Int32, String, String)
Constructs a new instance of the
SemVersion class.
Declaration
[Obsolete("This constructor is obsolete. Use another constructor or SemVersion.ParsedFrom() instead.")]
public SemVersion(int major, int minor = 0, int patch = 0, string prerelease = "", string build = "")
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
System.Int32 |
patch |
The patch version number. |
System.String |
prerelease |
The prerelease portion (e.g. "alpha.5"). |
System.String |
build |
The build metadata (e.g. "nightly.232"). |
SemVersion(Version)
Constructs a new instance of the
SemVersion class from
a
System.Version.
Declaration
[Obsolete("This constructor is obsolete. Use SemVersion.FromVersion() instead.")]
public SemVersion(Version version)
Parameters
Type |
Name |
Description |
System.Version |
version |
System.Version used to initialize
the major, minor, and patch version numbers and the build metadata. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
The version is null. |
Properties
Build
The build metadata for this version.
Declaration
[Obsolete("This property is obsolete. Use Metadata instead.")]
public string Build { get; }
Property Value
Type |
Description |
System.String |
The build metadata for this version or empty string if there is no build metadata.
|
IsPrerelease
Whether this is a prerelease version.
Declaration
public bool IsPrerelease { get; }
Property Value
Type |
Description |
System.Boolean |
Whether this is a prerelease version. A semantic version with
prerelease identifiers is a prerelease version. |
IsRelease
Whether this is a release version.
Declaration
public bool IsRelease { get; }
Property Value
Type |
Description |
System.Boolean |
Whether this is a release version. A semantic version without
prerelease identifiers is a release version. |
Major
The major version number.
Declaration
public int Major { get; }
Property Value
Type |
Description |
System.Int32 |
The major version number. |
The build metadata for this version.
Declaration
public string Metadata { get; }
Property Value
Type |
Description |
System.String |
The build metadata for this version or empty string if there
is no metadata. |
The build metadata identifiers for this version.
Declaration
public IReadOnlyList<MetadataIdentifier> MetadataIdentifiers { get; }
Property Value
Type |
Description |
System.Collections.Generic.IReadOnlyList<MetadataIdentifier> |
The build metadata identifiers for this version or empty if there
is no metadata. |
Minor
The minor version number.
Declaration
public int Minor { get; }
Property Value
Type |
Description |
System.Int32 |
The minor version number. |
Patch
The patch version number.
Declaration
public int Patch { get; }
Property Value
Type |
Description |
System.Int32 |
The patch version number. |
PrecedenceComparer
An
System.Collections.Generic.IEqualityComparer<T> and
System.Collections.Generic.IComparer<T>
that compares
SemVersion by precedence. This can be used for sorting,
binary search, and using
SemVersion as a dictionary key.
Declaration
public static ISemVersionComparer PrecedenceComparer { get; }
Property Value
Type |
Description |
ISemVersionComparer |
A precedence comparer that implements System.Collections.Generic.IEqualityComparer<T> and
System.Collections.Generic.IComparer<T> for SemVersion. |
Prerelease
The prerelease identifiers for this version.
Declaration
public string Prerelease { get; }
Property Value
Type |
Description |
System.String |
The prerelease identifiers for this version or empty string if this is a release version.
|
PrereleaseIdentifiers
The prerelease identifiers for this version.
Declaration
public IReadOnlyList<PrereleaseIdentifier> PrereleaseIdentifiers { get; }
Property Value
Type |
Description |
System.Collections.Generic.IReadOnlyList<PrereleaseIdentifier> |
The prerelease identifiers for this version or empty if this is a release version.
|
SortOrderComparer
An
System.Collections.Generic.IEqualityComparer<T> and
System.Collections.Generic.IComparer<T>
that compares
SemVersion by sort order. This can be used for sorting,
binary search, and using
SemVersion as a dictionary key.
Declaration
public static ISemVersionComparer SortOrderComparer { get; }
Property Value
Type |
Description |
ISemVersionComparer |
A sort order comparer that implements System.Collections.Generic.IEqualityComparer<T> and
System.Collections.Generic.IComparer<T> for SemVersion. |
Methods
Change(Nullable<Int32>, Nullable<Int32>, Nullable<Int32>, String, String)
Make a copy of the current instance with changed properties.
Declaration
[Obsolete("Method is obsolete. Use With() or With...() method instead.")]
public SemVersion Change(int? major = null, int? minor = null, int? patch = null, string prerelease = null, string build = null)
Parameters
Type |
Name |
Description |
System.Nullable<System.Int32> |
major |
The value to replace the major version number or
null to leave it unchanged. |
System.Nullable<System.Int32> |
minor |
The value to replace the minor version number or
null to leave it unchanged. |
System.Nullable<System.Int32> |
patch |
The value to replace the patch version number or
null to leave it unchanged. |
System.String |
prerelease |
The value to replace the prerelease portion
or null to leave it unchanged. |
System.String |
build |
The value to replace the build metadata or null
to leave it unchanged. |
Returns
Type |
Description |
SemVersion |
The new version with changed properties. |
Examples
To change only the patch version:
var changedVersion = version.Change(patch: 4);
Compare(SemVersion, SemVersion)
Compares two versions and indicates whether the first precedes, follows, or is
equal to the other in the sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Method is obsolete. Use CompareSortOrder() or ComparePrecedence() instead.")]
public static int Compare(SemVersion versionA, SemVersion versionB)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether versionA precedes, follows, or
is equal to versionB in the sort order.
Value | Condition |
---|
Less than zero | versionA precedes versionB in the sort order. | Zero | versionA is equal to versionB . | Greater than zero |
versionA follows versionB in the sort order
or versionB is null.
|
|
CompareByPrecedence(SemVersion)
Compares two versions and indicates whether this instance precedes, follows, or is in the same
position as the other in the precedence order. Versions that differ only by build metadata
have the same precedence.
Declaration
[Obsolete("Method is obsolete. Use ComparePrecedenceTo() or CompareSortOrderTo() instead.")]
public int CompareByPrecedence(SemVersion other)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether this instance precedes, follows, or is in the same
position as other in the precedence order.
Value | Condition |
---|
Less than zero | This instance precedes other in the precedence order. | Zero | This instance has the same precedence as other . | Greater than zero |
This instance follows other in the precedence order
or other is null.
|
|
ComparePrecedence(SemVersion, SemVersion)
Compares two versions and indicates whether the first precedes, follows, or is in the same
position as the second in the precedence order. Versions that differ only by build metadata
have the same precedence.
Declaration
public static int ComparePrecedence(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether left precedes, follows, or is in the same
position as right in the precedence order.
Value | Condition |
---|
-1 |
left precedes right in the precedence
order or left is null. | 0 | left has the same precedence as right . | 1 |
left follows right in the precedence order
or right is null.
|
|
ComparePrecedenceTo(SemVersion)
Compares two versions and indicates whether this instance precedes, follows, or is in the same
position as the other in the precedence order. Versions that differ only by build metadata
have the same precedence.
Declaration
public int ComparePrecedenceTo(SemVersion other)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether this instance precedes, follows, or is in the same
position as other in the precedence order.
Value | Condition |
---|
-1 | This instance precedes other in the precedence order. | 0 | This instance has the same precedence as other . | 1 |
This instance follows other in the precedence order
or other is null.
|
|
CompareSortOrder(SemVersion, SemVersion)
Compares two versions and indicates whether the first precedes, follows, or is equal to
the second in the sort order. Note that sort order is more specific than precedence order.
Declaration
public static int CompareSortOrder(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether left precedes, follows, or is equal
to right in the sort order.
Value | Condition |
---|
-1 |
left precedes right in the sort
order or left is null. | 0 | left is equal to right . | 1 |
left follows right in the sort order
or right is null.
|
|
CompareSortOrderTo(SemVersion)
Compares two versions and indicates whether this instance precedes, follows, or is equal
to the other in the sort order. Note that sort order is more specific than precedence order.
Declaration
public int CompareSortOrderTo(SemVersion other)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether this instance precedes, follows, or is equal to the
other in the sort order.
Value | Condition |
---|
-1 | This instance precedes the other in the sort order. | 0 | This instance is equal to the other. | 1 |
This instance follows the other in the sort order
or the other is null.
|
|
CompareTo(SemVersion)
Compares two versions and indicates whether this instance precedes, follows, or is
equal to the other in the sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Method is obsolete. Use CompareSortOrderTo() or ComparePrecedenceTo() instead.")]
public int CompareTo(SemVersion other)
Parameters
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether this instance precedes, follows, or is equal to
the other in the sort order.
Value | Condition |
---|
Less than zero | This instance precedes the other in the sort order. | Zero | This instance is equal to the other. | Greater than zero |
This instance follows the other in the sort order
or the other is null.
|
|
CompareTo(Object)
Compares this version to an System.Object and indicates whether this instance
precedes, follows, or is equal to the object in the sort order. Note that sort order
is more specific than precedence order.
Declaration
[Obsolete("Method is obsolete. Use CompareSortOrderTo() or ComparePrecedenceTo() instead.")]
public int CompareTo(object obj)
Parameters
Type |
Name |
Description |
System.Object |
obj |
|
Returns
Type |
Description |
System.Int32 |
An integer that indicates whether this instance precedes, follows, or is equal to
the other in the sort order.
Value | Condition |
---|
Less than zero | This instance precedes the other in the sort order. | Zero | This instance is equal to the other. | Greater than zero |
This instance follows the other in the sort order
or the other is null.
|
|
Exceptions
Type |
Condition |
System.InvalidCastException |
The obj is not a SemVersion. |
Equals(SemVersion)
Determines whether two semantic versions are equal.
Declaration
public bool Equals(SemVersion other)
Parameters
Returns
Type |
Description |
System.Boolean |
true if other is equal to the this version;
otherwise false. |
Equals(SemVersion, SemVersion)
Determines whether two semantic versions are equal.
Declaration
public static bool Equals(SemVersion versionA, SemVersion versionB)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the two versions are equal, otherwise false. |
Equals(Object)
Determines whether the given object is equal to this version.
Declaration
public override bool Equals(object obj)
Parameters
Type |
Name |
Description |
System.Object |
obj |
|
Returns
Type |
Description |
System.Boolean |
true if obj is equal to the this version;
otherwise false. |
Overrides
System.Object.Equals(System.Object)
FromVersion(Version)
Converts a System.Version into the equivalent semantic version.
Declaration
public static SemVersion FromVersion(Version version)
Parameters
Type |
Name |
Description |
System.Version |
version |
The version to be converted to a semantic version. |
Returns
Type |
Description |
SemVersion |
The equivalent semantic version. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
version is null. |
System.ArgumentException |
version has a revision number greater than zero. |
GetHashCode()
Gets a hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type |
Description |
System.Int32 |
A hash code for this instance, suitable for use in hashing algorithms
and data structures like a hash table.
|
Overrides
System.Object.GetHashCode()
GetObjectData(SerializationInfo, StreamingContext)
Populates a System.Runtime.Serialization.SerializationInfo with the data needed to serialize the target object.
Declaration
public void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
Type |
Name |
Description |
System.Runtime.Serialization.SerializationInfo |
info |
The System.Runtime.Serialization.SerializationInfo to populate with data. |
System.Runtime.Serialization.StreamingContext |
context |
The destination (see System.Runtime.Serialization.SerializationInfo) for this serialization. |
Parse(String, SemVersionStyles, Int32)
Converts the string representation of a semantic version to its
SemVersion equivalent.
Declaration
public static SemVersion Parse(string version, SemVersionStyles style, int maxLength = 1024)
Parameters
Type |
Name |
Description |
System.String |
version |
The version string. |
SemVersionStyles |
style |
A bitwise combination of enumeration values that indicates the style
elements that can be present in version . The preferred value to use
is Strict. |
System.Int32 |
maxLength |
The maximum length of version that should be
parsed. This prevents attacks using very long version strings. |
Returns
Exceptions
Type |
Condition |
System.ArgumentException |
style is not a valid
SemVersionStyles value. |
System.ArgumentNullException |
version is null. |
System.FormatException |
The version is invalid or not in a
format compliant with style . |
System.OverflowException |
A numeric part of version is too
large for an System.Int32. |
Parse(String, Boolean)
Converts the string representation of a semantic version to its
SemVersion equivalent.
Declaration
[Obsolete("Method is obsolete. Use Parse() overload with SemVersionStyles instead.")]
public static SemVersion Parse(string version, bool strict = false)
Parameters
Type |
Name |
Description |
System.String |
version |
The version string. |
System.Boolean |
strict |
If set to true, minor and patch version are required;
otherwise they are optional. |
Returns
Exceptions
Type |
Condition |
System.ArgumentNullException |
The version is null. |
System.ArgumentException |
The version has an invalid format. |
System.InvalidOperationException |
The version is missing minor
or patch version numbers when strict is true. |
System.OverflowException |
The major, minor, or patch version number is larger
than System.Int32.MaxValue. |
ParsedFrom(Int32, Int32, Int32, String, String, Boolean)
Create a new instance of the
SemVersion class. Parses prerelease
and metadata identifiers from dot separated strings. If parsing is not needed, use a
constructor instead.
Declaration
public static SemVersion ParsedFrom(int major, int minor = 0, int patch = 0, string prerelease = "", string metadata = "", bool allowLeadingZeros = false)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The major version number. |
System.Int32 |
minor |
The minor version number. |
System.Int32 |
patch |
The patch version number. |
System.String |
prerelease |
The prerelease portion (e.g. "alpha.5"). |
System.String |
metadata |
The build metadata (e.g. "nightly.232"). |
System.Boolean |
allowLeadingZeros |
Allow leading zeros in numeric prerelease identifiers. Leading
zeros will be removed. |
Returns
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
A major ,
minor , or patch version number is negative. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier when allowLeadingZeros is
false. Or, a metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
PrecedenceEquals(SemVersion)
Determines whether two semantic versions have the same precedence. Versions that differ
only by build metadata have the same precedence.
Declaration
public bool PrecedenceEquals(SemVersion other)
Parameters
Type |
Name |
Description |
SemVersion |
other |
The semantic version to compare to. |
Returns
Type |
Description |
System.Boolean |
true if the version precedences are equal, otherwise
false. |
PrecedenceEquals(SemVersion, SemVersion)
Determines whether two semantic versions have the same precedence. Versions that differ
only by build metadata have the same precedence.
Declaration
public static bool PrecedenceEquals(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the version precedences are equal, otherwise
false. |
PrecedenceMatches(SemVersion)
Determines whether two semantic versions have the same precedence. Versions
that differ only by build metadata have the same precedence.
Declaration
[Obsolete("Method is obsolete. Use PrecedenceEquals() instead.")]
public bool PrecedenceMatches(SemVersion other)
Parameters
Type |
Name |
Description |
SemVersion |
other |
The semantic version to compare to. |
Returns
Type |
Description |
System.Boolean |
true if the version precedences are equal. |
Satisfies(SemVersionRange)
Checks if this version is contained in the given range.
Declaration
public bool Satisfies(SemVersionRange range)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
Satisfies(UnbrokenSemVersionRange)
Checks if this version is contained in the given unbroken range.
Declaration
public bool Satisfies(UnbrokenSemVersionRange range)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
Satisfies(Predicate<SemVersion>)
Checks if this version satisfies the given predicate.
Declaration
public bool Satisfies(Predicate<SemVersion> predicate)
Parameters
Type |
Name |
Description |
System.Predicate<SemVersion> |
predicate |
The predicate to evaluate on this version. |
Returns
Type |
Description |
System.Boolean |
true if the version satisfies the predicate,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
predicate is
null. |
Satisfies(String, SemVersionRangeOptions, Int32)
Checks if this version is contained in the given range.
Declaration
public bool Satisfies(string range, SemVersionRangeOptions options, int maxLength = 2048)
Parameters
Type |
Name |
Description |
System.String |
range |
The range to parse and evaluate. |
SemVersionRangeOptions |
options |
A bitwise combination of enumeration values that indicates the style
elements that can be present in range . The overload without this
parameter defaults to Strict. |
System.Int32 |
maxLength |
The maximum length of range that should be
parsed. This prevents attacks using very long range strings. |
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
System.ArgumentException |
options is not a valid
SemVersionRangeOptions value. |
System.ArgumentOutOfRangeException |
maxLength is less than
zero. |
System.FormatException |
The range is invalid or not in a
format compliant with options . |
System.OverflowException |
A numeric part of a version in range
is too large for an System.Int32. |
Satisfies(String, Int32)
Checks if this version is contained in the given range. The range is parsed using
Strict.
Declaration
public bool Satisfies(string range, int maxLength = 2048)
Parameters
Type |
Name |
Description |
System.String |
range |
The range to parse and evaluate. |
System.Int32 |
maxLength |
The maximum length of range that should be
parsed. This prevents attacks using very long range strings. |
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
System.ArgumentOutOfRangeException |
maxLength is less than
zero. |
System.FormatException |
The range is invalid or not in a
format compliant with Strict. |
System.OverflowException |
A numeric part of a version in range
is too large for an System.Int32. |
SatisfiesNpm(String, Boolean, Int32)
Checks if this version is contained in the given range in npm format.
Declaration
public bool SatisfiesNpm(string range, bool includeAllPrerelease, int maxLength = 2048)
Parameters
Type |
Name |
Description |
System.String |
range |
The npm format range to parse and evaluate. |
System.Boolean |
includeAllPrerelease |
Whether to include all prerelease versions satisfying
the bounds in the range or to only include prerelease versions when it matches a bound
that explicitly includes prerelease versions. |
System.Int32 |
maxLength |
The maximum length of range that should be
parsed. This prevents attacks using very long range strings. |
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
System.ArgumentOutOfRangeException |
maxLength is less than
zero. |
System.FormatException |
The range is invalid. |
System.OverflowException |
A numeric part of a version in range
is too large for an System.Int32. |
SatisfiesNpm(String, Int32)
Checks if this version is contained in the given range in npm format. Does not include
all prerelease when parsing the range.
Declaration
public bool SatisfiesNpm(string range, int maxLength = 2048)
Parameters
Type |
Name |
Description |
System.String |
range |
The npm format range to parse and evaluate. |
System.Int32 |
maxLength |
The maximum length of range that should be
parsed. This prevents attacks using very long range strings. |
Returns
Type |
Description |
System.Boolean |
true if the version is contained in the range,
otherwise false. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
range is
null. |
System.ArgumentOutOfRangeException |
maxLength is less than
zero. |
System.FormatException |
The range is invalid. |
System.OverflowException |
A numeric part of a version in range
is too large for an System.Int32. |
ToString()
Converts this version to an equivalent string value.
Declaration
public override string ToString()
Returns
Type |
Description |
System.String |
The System.String equivalent of this version.
|
Overrides
System.Object.ToString()
ToVersion()
Converts this semantic version to a System.Version.
Declaration
public Version ToVersion()
Returns
Type |
Description |
System.Version |
The equivalent System.Version. |
Exceptions
Type |
Condition |
System.InvalidOperationException |
The semantic version is a prerelease version
or has build metadata or has a negative major, minor, or patch version number. |
TryParse(String, out SemVersion, Boolean)
Converts the string representation of a semantic version to its
SemVersion
equivalent. The return value indicates whether the conversion succeeded.
Declaration
[Obsolete("Method is obsolete. Use TryParse() overload with SemVersionStyles instead.")]
public static bool TryParse(string version, out SemVersion semver, bool strict = false)
Parameters
Type |
Name |
Description |
System.String |
version |
The version string. |
SemVersion |
semver |
When this method returns, contains a SemVersion instance equivalent
to the version string passed in, if the version string was valid, or null if the
version string was invalid. |
System.Boolean |
strict |
If set to true, minor and patch version numbers are required;
otherwise they are optional. |
Returns
Type |
Description |
System.Boolean |
false when an invalid version string is passed, otherwise true. |
TryParse(String, SemVersionStyles, out SemVersion, Int32)
Converts the string representation of a semantic version to its
SemVersion
equivalent. The return value indicates whether the conversion succeeded.
Declaration
public static bool TryParse(string version, SemVersionStyles style, out SemVersion semver, int maxLength = 1024)
Parameters
Type |
Name |
Description |
System.String |
version |
The version string. |
SemVersionStyles |
style |
A bitwise combination of enumeration values that indicates the style
elements that can be present in version . The preferred value to use
is Strict. |
SemVersion |
semver |
When this method returns, contains a SemVersion instance equivalent
to the version string passed in, if the version string was valid, or null if the
version string was invalid. |
System.Int32 |
maxLength |
The maximum length of version that should be
parsed. This prevents attacks using very long version strings. |
Returns
Type |
Description |
System.Boolean |
false when an invalid version string is passed, otherwise true. |
Exceptions
Type |
Condition |
System.ArgumentException |
style is not a valid
SemVersionStyles value. |
Creates a copy of the current instance with multiple changed properties. If changing only
one property use one of the more specific WithX()
methods.
Declaration
public SemVersion With(int? major = null, int? minor = null, int? patch = null, IEnumerable<PrereleaseIdentifier> prerelease = null, IEnumerable<MetadataIdentifier> metadata = null)
Parameters
Type |
Name |
Description |
System.Nullable<System.Int32> |
major |
The value to replace the major version number or null to leave it unchanged. |
System.Nullable<System.Int32> |
minor |
The value to replace the minor version number or null to leave it unchanged. |
System.Nullable<System.Int32> |
patch |
The value to replace the patch version number or null to leave it unchanged. |
System.Collections.Generic.IEnumerable<PrereleaseIdentifier> |
prerelease |
The value to replace the prerelease identifiers or null to leave it unchanged. |
System.Collections.Generic.IEnumerable<MetadataIdentifier> |
metadata |
The value to replace the build metadata identifiers or null to leave it unchanged. |
Returns
Type |
Description |
SemVersion |
The new version with changed properties. |
To change the minor and patch versions:
var modifiedVersion = version.With(minor: 2, patch: 4);
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
A major ,
minor , or patch version number is negative. |
System.ArgumentException |
A prerelease or metadata identifier has the default value. |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
WithMajor(Int32)
Creates a copy of the current instance with a different major version number.
Declaration
public SemVersion WithMajor(int major)
Parameters
Type |
Name |
Description |
System.Int32 |
major |
The value to replace the major version number. |
Returns
Type |
Description |
SemVersion |
The new version with the different major version number. |
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
major is negative. |
Creates a copy of the current instance with different build metadata identifiers.
Declaration
public SemVersion WithMetadata(MetadataIdentifier metadataIdentifier, params MetadataIdentifier[] metadataIdentifiers)
Parameters
Type |
Name |
Description |
MetadataIdentifier |
metadataIdentifier |
The first identifier to replace the existing
build metadata identifiers. |
MetadataIdentifier[] |
metadataIdentifiers |
The rest of the identifiers to replace the
existing build metadata identifiers. |
Returns
Exceptions
Type |
Condition |
System.ArgumentNullException |
metadataIdentifiers is
null. |
System.ArgumentException |
A metadata identifier has the default value. |
Creates a copy of the current instance with different build metadata identifiers.
Declaration
public SemVersion WithMetadata(IEnumerable<MetadataIdentifier> metadataIdentifiers)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<MetadataIdentifier> |
metadataIdentifiers |
The identifiers to replace the build metadata identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different build metadata identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
metadataIdentifiers is
null. |
System.ArgumentException |
A metadata identifier has the default value. |
Creates a copy of the current instance with different build metadata identifiers.
Declaration
public SemVersion WithMetadata(IEnumerable<string> metadataIdentifiers)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<System.String> |
metadataIdentifiers |
The identifiers to replace the build metadata identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different build metadata identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
metadataIdentifiers is
null or one of the metadata identifiers is null. |
System.ArgumentException |
A metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
Creates a copy of the current instance with different build metadata identifiers.
Declaration
public SemVersion WithMetadata(string metadataIdentifier, params string[] metadataIdentifiers)
Parameters
Type |
Name |
Description |
System.String |
metadataIdentifier |
The first identifier to replace the existing
build metadata identifiers. |
System.String[] |
metadataIdentifiers |
The rest of the build metadata identifiers to replace the
existing build metadata identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different build metadata identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
metadataIdentifier or
metadataIdentifiers is null or one of the metadata
identifiers is null. |
System.ArgumentException |
A metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
Creates a copy of the current instance with different build metadata.
Declaration
public SemVersion WithMetadataParsedFrom(string metadata)
Parameters
Type |
Name |
Description |
System.String |
metadata |
The value to replace the build metadata. |
Returns
Type |
Description |
SemVersion |
The new version with the different build metadata. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
metadata is null. |
System.ArgumentException |
A metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
WithMinor(Int32)
Creates a copy of the current instance with a different minor version number.
Declaration
public SemVersion WithMinor(int minor)
Parameters
Type |
Name |
Description |
System.Int32 |
minor |
The value to replace the minor version number. |
Returns
Type |
Description |
SemVersion |
The new version with the different minor version number. |
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
minor is negative. |
Creates a copy of the current instance without build metadata.
Declaration
public SemVersion WithoutMetadata()
Returns
Type |
Description |
SemVersion |
The new version without build metadata. |
WithoutPrerelease()
Creates a copy of the current instance without prerelease identifiers.
Declaration
public SemVersion WithoutPrerelease()
Returns
Type |
Description |
SemVersion |
The new version without prerelease identifiers. |
Creates a copy of the current instance without prerelease identifiers or build metadata.
Declaration
public SemVersion WithoutPrereleaseOrMetadata()
Returns
Type |
Description |
SemVersion |
The new version without prerelease identifiers or build metadata. |
WithParsedFrom(Nullable<Int32>, Nullable<Int32>, Nullable<Int32>, String, String, Boolean)
Declaration
public SemVersion WithParsedFrom(int? major = null, int? minor = null, int? patch = null, string prerelease = null, string metadata = null, bool allowLeadingZeros = false)
Parameters
Type |
Name |
Description |
System.Nullable<System.Int32> |
major |
The value to replace the major version number or null to leave it unchanged. |
System.Nullable<System.Int32> |
minor |
The value to replace the minor version number or null to leave it unchanged. |
System.Nullable<System.Int32> |
patch |
The value to replace the patch version number or null to leave it unchanged. |
System.String |
prerelease |
The value to replace the prerelease identifiers or null to leave it unchanged. |
System.String |
metadata |
The value to replace the build metadata identifiers or null to leave it unchanged. |
System.Boolean |
allowLeadingZeros |
Allow leading zeros in numeric prerelease identifiers. Leading
zeros will be removed. |
Returns
Type |
Description |
SemVersion |
The new version with changed properties. |
Examples
To change the patch version and prerelease identifiers version:
var modifiedVersion = version.WithParsedFrom(patch: 4, prerelease: "alpha.5");
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
A major ,
minor , or patch version number is negative. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier when allowLeadingZeros is
false. Or, a metadata identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens). |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
WithPatch(Int32)
Creates a copy of the current instance with a different patch version number.
Declaration
public SemVersion WithPatch(int patch)
Parameters
Type |
Name |
Description |
System.Int32 |
patch |
The value to replace the patch version number. |
Returns
Type |
Description |
SemVersion |
The new version with the different patch version number. |
Exceptions
Type |
Condition |
System.ArgumentOutOfRangeException |
patch is negative. |
WithPrerelease(PrereleaseIdentifier, PrereleaseIdentifier[])
Creates a copy of the current instance with different prerelease identifiers.
Declaration
public SemVersion WithPrerelease(PrereleaseIdentifier prereleaseIdentifier, params PrereleaseIdentifier[] prereleaseIdentifiers)
Parameters
Type |
Name |
Description |
PrereleaseIdentifier |
prereleaseIdentifier |
The first identifier to replace the existing
prerelease identifiers. |
PrereleaseIdentifier[] |
prereleaseIdentifiers |
The rest of the identifiers to replace the
existing prerelease identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different prerelease identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
prereleaseIdentifiers is
null. |
System.ArgumentException |
A prerelease identifier has the default value. |
WithPrerelease(IEnumerable<PrereleaseIdentifier>)
Creates a copy of the current instance with different prerelease identifiers.
Declaration
public SemVersion WithPrerelease(IEnumerable<PrereleaseIdentifier> prereleaseIdentifiers)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<PrereleaseIdentifier> |
prereleaseIdentifiers |
The identifiers to replace the prerelease identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different prerelease identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
prereleaseIdentifiers is null. |
System.ArgumentException |
A prerelease identifier has the default value. |
WithPrerelease(IEnumerable<String>)
Creates a copy of the current instance with different prerelease identifiers.
Declaration
public SemVersion WithPrerelease(IEnumerable<string> prereleaseIdentifiers)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<System.String> |
prereleaseIdentifiers |
The identifiers to replace the prerelease identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different prerelease identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
prereleaseIdentifiers is
null or one of the prerelease identifiers is null. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier. |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
WithPrerelease(String, String[])
Creates a copy of the current instance with different prerelease identifiers.
Declaration
public SemVersion WithPrerelease(string prereleaseIdentifier, params string[] prereleaseIdentifiers)
Parameters
Type |
Name |
Description |
System.String |
prereleaseIdentifier |
The first identifier to replace the existing
prerelease identifiers. |
System.String[] |
prereleaseIdentifiers |
The rest of the identifiers to replace the
existing prerelease identifiers. |
Returns
Type |
Description |
SemVersion |
The new version with the different prerelease identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
prereleaseIdentifier or
prereleaseIdentifiers is null or one of the
prerelease identifiers is null. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier. |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
WithPrereleaseParsedFrom(String, Boolean)
Creates a copy of the current instance with a different prerelease portion.
Declaration
public SemVersion WithPrereleaseParsedFrom(string prerelease, bool allowLeadingZeros = false)
Parameters
Type |
Name |
Description |
System.String |
prerelease |
The value to replace the prerelease portion. |
System.Boolean |
allowLeadingZeros |
Whether to allow leading zeros in the prerelease identifiers.
If true, leading zeros will be allowed on numeric identifiers
but will be removed. |
Returns
Type |
Description |
SemVersion |
The new version with the different prerelease identifiers. |
Exceptions
Type |
Condition |
System.ArgumentNullException |
prerelease is null. |
System.ArgumentException |
A prerelease identifier is empty or contains invalid
characters (i.e. characters that are not ASCII alphanumerics or hyphens) or has leading
zeros for a numeric identifier when allowLeadingZeros is false. |
System.OverflowException |
A numeric prerelease identifier value is too large
for System.Int32. |
Operators
Equality(SemVersion, SemVersion)
Determines whether two semantic versions are equal.
Declaration
public static bool operator ==(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the two versions are equal, otherwise false. |
GreaterThan(SemVersion, SemVersion)
Compares two versions by sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Operator is obsolete. Use CompareSortOrder() or ComparePrecedence() instead.")]
public static bool operator>(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if left follows right
in the sort order; otherwise false. |
GreaterThanOrEqual(SemVersion, SemVersion)
Compares two versions by sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Operator is obsolete. Use CompareSortOrder() or ComparePrecedence() instead.")]
public static bool operator >=(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if left follows or is equal to
right in the sort order; otherwise false. |
Implicit(String to SemVersion)
Implicit conversion from
System.String to
SemVersion.
Declaration
[Obsolete("Implicit conversion from string is obsolete. Use Parse() or TryParse() method instead.")]
public static implicit operator SemVersion(string version)
Parameters
Type |
Name |
Description |
System.String |
version |
The semantic version. |
Returns
Exceptions
Type |
Condition |
System.ArgumentNullException |
The version is null. |
System.ArgumentException |
The version number has an invalid format. |
System.OverflowException |
The major, minor, or patch version number is larger than System.Int32.MaxValue. |
Inequality(SemVersion, SemVersion)
Determines whether two semantic versions are not equal.
Declaration
public static bool operator !=(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if the two versions are not equal, otherwise false. |
LessThan(SemVersion, SemVersion)
Compares two versions by sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Operator is obsolete. Use CompareSortOrder() or ComparePrecedence() instead.")]
public static bool operator <(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if left precedes right
in the sort order; otherwise false. |
LessThanOrEqual(SemVersion, SemVersion)
Compares two versions by sort order. Note that sort order is more specific than precedence order.
Declaration
[Obsolete("Operator is obsolete. Use CompareSortOrder() or ComparePrecedence() instead.")]
public static bool operator <=(SemVersion left, SemVersion right)
Parameters
Returns
Type |
Description |
System.Boolean |
true if left precedes or is equal to
right in the sort order; otherwise false. |
Implements
System.IComparable<T>
System.IComparable
System.IEquatable<T>
System.Runtime.Serialization.ISerializable