SemanticVersionRange Class
Represents a range of semantic versions with optional lower and upper bounds. Each bound can be inclusive or exclusive.
public sealed class SemanticVersionRange : IEquatable<SemanticVersionRange>Example
// NuGet format examples
var range1 = SemanticVersionRange.ParseNuGet("[1.0.0, 2.0.0)"); // >=1.0.0 and <2.0.0
var range2 = SemanticVersionRange.ParseNuGet("1.0.0"); // >=1.0.0
var range3 = SemanticVersionRange.ParseNuGet("[1.0.0]"); // ==1.0.0
Assert.Equal(SemanticVersionRange.GreaterThanOrEqual(SemanticVersion.Parse("1.0.0")), range2);
Assert.Equal(SemanticVersionRange.Exact(SemanticVersion.Parse("1.0.0")), range3);
// npm format examples
var range4 = SemanticVersionRange.ParseNpm(">=1.0.0 <2.0.0"); // >=1.0.0 and <2.0.0
var range5 = SemanticVersionRange.ParseNpm("<=2.0.0"); // <=2.0.0
Assert.Equal(range1, range4);
Assert.Equal(SemanticVersionRange.LessThanOrEqual(SemanticVersion.Parse("2.0.0")), range5);
// Check if a version satisfies the range
var version = SemanticVersion.Parse("1.5.0");
Assert.True(range1.Satisfies(version));- Implements
Constructors
| Name | Description |
|---|---|
| SemanticVersionRange(SemanticVersion?, SemanticVersion?, bool, bool) | Creates a new semantic version range with the specified bounds. |
SemanticVersionRange(SemanticVersion?, SemanticVersion?, bool, bool)
SemanticVersionRange.csCreates a new semantic version range with the specified bounds.
public SemanticVersionRange(SemanticVersion? minVersion, SemanticVersion? maxVersion, bool isMinInclusive, bool isMaxInclusive)Parameters
minVersionSemanticVersion?- The minimum version bound, or null for no lower bound.
maxVersionSemanticVersion?- The maximum version bound, or null for no upper bound.
isMinInclusivebool- Whether the minimum bound is inclusive.
isMaxInclusivebool- Whether the maximum bound is inclusive.
Properties
| Name | Description |
|---|---|
| All | Gets a range that matches all versions. |
| IsMaxInclusive | Gets a value indicating whether the maximum bound is inclusive. |
| IsMinInclusive | Gets a value indicating whether the minimum bound is inclusive. |
| MaxVersion | Gets the maximum version bound, or null if there is no upper bound. |
| MinVersion | Gets the minimum version bound, or null if there is no lower bound. |
Gets a range that matches all versions.
public static SemanticVersionRange All { get; }Property value
IsMaxInclusive
SemanticVersionRange.csIsMinInclusive
SemanticVersionRange.csMaxVersion
SemanticVersionRange.csGets the maximum version bound, or null if there is no upper bound.
public SemanticVersion? MaxVersion { get; }Property value
MinVersion
SemanticVersionRange.csGets the minimum version bound, or null if there is no lower bound.
public SemanticVersion? MinVersion { get; }Property value
Methods
| Name | Description |
|---|---|
| Equals(SemanticVersionRange?) | |
| Equals(object?) | |
| Exact(SemanticVersion) | Creates a range that matches only the exact specified version. |
| GetHashCode() | |
| GreaterThan(SemanticVersion) | Creates a range that matches versions greater than the specified version. |
| GreaterThanOrEqual(SemanticVersion) | Creates a range that matches versions greater than or equal to the specified version. |
| LessThan(SemanticVersion) | Creates a range that matches versions less than the specified version. |
| LessThanOrEqual(SemanticVersion) | Creates a range that matches versions less than or equal to the specified version. |
| ParseNpm(ReadOnlySpan<char>) | Parses a version range in npm format. |
| ParseNpm(string) | Parses a version range in npm format. |
| ParseNuGet(ReadOnlySpan<char>) | Parses a version range in NuGet format. |
| ParseNuGet(string) | Parses a version range in NuGet format. |
| Satisfies(SemanticVersion) | Determines whether the specified version satisfies this range. |
| ToString() | |
| TryParseNpm(ReadOnlySpan<char>, SemanticVersionRange?) | Attempts to parse a version range in npm format. |
| TryParseNpm(string?, SemanticVersionRange?) | Attempts to parse a version range in npm format. |
| TryParseNuGet(ReadOnlySpan<char>, SemanticVersionRange?) | Attempts to parse a version range in NuGet format. |
| TryParseNuGet(string?, SemanticVersionRange?) | Attempts to parse a version range in NuGet format. |
Equals overloads
Equals(SemanticVersionRange?)
SemanticVersionRange.cspublic bool Equals(SemanticVersionRange? other)No documentation is available.
Equals(object?)
SemanticVersionRange.csExact(SemanticVersion)
SemanticVersionRange.csCreates a range that matches only the exact specified version.
public static SemanticVersionRange Exact(SemanticVersion version)Parameters
versionSemanticVersion- The exact version to match.
Returns
SemanticVersionRange A range that matches only the specified version.
GetHashCode()
SemanticVersionRange.csGreaterThan(SemanticVersion)
SemanticVersionRange.csCreates a range that matches versions greater than the specified version.
public static SemanticVersionRange GreaterThan(SemanticVersion version)Parameters
versionSemanticVersion- The minimum version (exclusive).
Returns
SemanticVersionRange A range that matches versions > the specified version.
GreaterThanOrEqual(SemanticVersion)
SemanticVersionRange.csCreates a range that matches versions greater than or equal to the specified version.
public static SemanticVersionRange GreaterThanOrEqual(SemanticVersion version)Parameters
versionSemanticVersion- The minimum version (inclusive).
Returns
SemanticVersionRange A range that matches versions >= the specified version.
LessThan(SemanticVersion)
SemanticVersionRange.csCreates a range that matches versions less than the specified version.
public static SemanticVersionRange LessThan(SemanticVersion version)Parameters
versionSemanticVersion- The maximum version (exclusive).
Returns
SemanticVersionRange A range that matches versions < the specified version.
LessThanOrEqual(SemanticVersion)
SemanticVersionRange.csCreates a range that matches versions less than or equal to the specified version.
public static SemanticVersionRange LessThanOrEqual(SemanticVersion version)Parameters
versionSemanticVersion- The maximum version (inclusive).
Returns
SemanticVersionRange A range that matches versions <= the specified version.
ParseNpm overloads
ParseNpm(ReadOnlySpan<char>)
SemanticVersionRange.csParses a version range in npm format.
public static SemanticVersionRange ParseNpm(ReadOnlySpan<char> value)Parameters
valueReadOnlySpan<char>- The span to parse.
Returns
SemanticVersionRange The parsed version range.
Exceptions
FormatException- The value is not a valid npm version range.
See also
ParseNpm(string)
SemanticVersionRange.csParses a version range in npm format.
public static SemanticVersionRange ParseNpm(string value)Parameters
valuestring- The string to parse.
Returns
SemanticVersionRange The parsed version range.
Exceptions
ArgumentNullExceptionvalueis null.FormatException- The value is not a valid npm version range.
Remarks
Supported formats:
1.0.0- Exact version=1.0.0- Exact version>1.0.0- Greater than>=1.0.0- Greater than or equal<1.0.0- Less than<=1.0.0- Less than or equal>=1.0.0 <2.0.0- Range with multiple constraints (space-separated)~1.2.3- Tilde range: allows patch-level changes (>=1.2.3 <1.3.0)~1.2- Tilde range (>=1.2.0 <1.3.0)~1- Tilde range (>=1.0.0 <2.0.0)^1.2.3- Caret range: allows changes that don't modify left-most non-zero (>=1.2.3 <2.0.0)^0.2.3- Caret range (>=0.2.3 <0.3.0)^0.0.3- Caret range (>=0.0.3 <0.0.4)1.0.0 - 2.0.0- Hyphen range (>=1.0.0 <=2.0.0)*- Any version1.x- X-range: any minor/patch (>=1.0.0 <2.0.0)1.2.x- X-range: any patch (>=1.2.0 <1.3.0)
ParseNuGet overloads
ParseNuGet(ReadOnlySpan<char>)
SemanticVersionRange.csParses a version range in NuGet format.
public static SemanticVersionRange ParseNuGet(ReadOnlySpan<char> value)Parameters
valueReadOnlySpan<char>- The span to parse.
Returns
SemanticVersionRange The parsed version range.
Exceptions
FormatException- The value is not a valid NuGet version range.
ParseNuGet(string)
SemanticVersionRange.csParses a version range in NuGet format.
public static SemanticVersionRange ParseNuGet(string value)Parameters
valuestring- The string to parse.
Returns
SemanticVersionRange The parsed version range.
Exceptions
ArgumentNullExceptionvalueis null.FormatException- The value is not a valid NuGet version range.
Remarks
Supported formats:
1.0.0- Minimum version inclusive (>=1.0.0)[1.0.0]- Exact version (==1.0.0)(1.0.0,)- Greater than (>1.0.0)[1.0.0,)- Greater than or equal (>=1.0.0)(,1.0.0]- Less than or equal (<=1.0.0)(,1.0.0)- Less than (<1.0.0)[1.0.0,2.0.0]- Range inclusive on both ends[1.0.0,2.0.0)- Range inclusive on min, exclusive on max(1.0.0,2.0.0)- Range exclusive on both ends
Satisfies(SemanticVersion)
SemanticVersionRange.csDetermines whether the specified version satisfies this range.
public bool Satisfies(SemanticVersion version)Parameters
versionSemanticVersion- The version to check.
Returns
bool true if the version satisfies the range; otherwise, false.
ToString()
SemanticVersionRange.csTryParseNpm overloads
TryParseNpm(ReadOnlySpan<char>, SemanticVersionRange?)
SemanticVersionRange.csAttempts to parse a version range in npm format.
public static bool TryParseNpm(ReadOnlySpan<char> value, out SemanticVersionRange? result)Parameters
valueReadOnlySpan<char>- The span to parse.
resultSemanticVersionRange?- When this method returns, contains the parsed version range if the parse succeeded.
Returns
bool true if the parse succeeded; otherwise, false.
See also
TryParseNpm(string?, SemanticVersionRange?)
SemanticVersionRange.csAttempts to parse a version range in npm format.
public static bool TryParseNpm(string? value, out SemanticVersionRange? result)Parameters
valuestring?- The string to parse.
resultSemanticVersionRange?- When this method returns, contains the parsed version range if the parse succeeded.
Returns
bool true if the parse succeeded; otherwise, false.
See also
TryParseNuGet overloads
TryParseNuGet(ReadOnlySpan<char>, SemanticVersionRange?)
SemanticVersionRange.csAttempts to parse a version range in NuGet format.
public static bool TryParseNuGet(ReadOnlySpan<char> value, out SemanticVersionRange? result)Parameters
valueReadOnlySpan<char>- The span to parse.
resultSemanticVersionRange?- When this method returns, contains the parsed version range if the parse succeeded.
Returns
bool true if the parse succeeded; otherwise, false.
TryParseNuGet(string?, SemanticVersionRange?)
SemanticVersionRange.csAttempts to parse a version range in NuGet format.
public static bool TryParseNuGet(string? value, out SemanticVersionRange? result)Parameters
valuestring?- The string to parse.
resultSemanticVersionRange?- When this method returns, contains the parsed version range if the parse succeeded.
Returns
bool true if the parse succeeded; otherwise, false.
Operators
| Name | Description |
|---|---|
| operator ==(SemanticVersionRange?, SemanticVersionRange?) | |
| operator !=(SemanticVersionRange?, SemanticVersionRange?) |
operator ==(SemanticVersionRange?, SemanticVersionRange?)
SemanticVersionRange.cspublic static bool operator ==(SemanticVersionRange? left, SemanticVersionRange? right)No documentation is available.
operator !=(SemanticVersionRange?, SemanticVersionRange?)
SemanticVersionRange.cspublic static bool operator !=(SemanticVersionRange? left, SemanticVersionRange? right)No documentation is available.