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

NameDescription
SemanticVersionRange(SemanticVersion?, SemanticVersion?, bool, bool)Creates a new semantic version range with the specified bounds.

SemanticVersionRange(SemanticVersion?, SemanticVersion?, bool, bool)

SemanticVersionRange.cs

Creates a new semantic version range with the specified bounds.

public SemanticVersionRange(SemanticVersion? minVersion, SemanticVersion? maxVersion, bool isMinInclusive, bool isMaxInclusive)

Parameters

minVersion SemanticVersion?
The minimum version bound, or null for no lower bound.
maxVersion SemanticVersion?
The maximum version bound, or null for no upper bound.
isMinInclusive bool
Whether the minimum bound is inclusive.
isMaxInclusive bool
Whether the maximum bound is inclusive.

Properties

NameDescription
AllGets a range that matches all versions.
IsMaxInclusiveGets a value indicating whether the maximum bound is inclusive.
IsMinInclusiveGets a value indicating whether the minimum bound is inclusive.
MaxVersionGets the maximum version bound, or null if there is no upper bound.
MinVersionGets 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

SemanticVersionRange

Gets a value indicating whether the maximum bound is inclusive.

public bool IsMaxInclusive { get; }

Property value

bool

Gets a value indicating whether the minimum bound is inclusive.

public bool IsMinInclusive { get; }

Property value

bool

Gets the maximum version bound, or null if there is no upper bound.

public SemanticVersion? MaxVersion { get; }

Property value

SemanticVersion?

Gets the minimum version bound, or null if there is no lower bound.

public SemanticVersion? MinVersion { get; }

Property value

SemanticVersion?

Methods

NameDescription
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.cs
public bool Equals(SemanticVersionRange? other)
Parameters
other SemanticVersionRange?
Returns

bool

No documentation is available.

public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

Exact(SemanticVersion)

SemanticVersionRange.cs

Creates a range that matches only the exact specified version.

public static SemanticVersionRange Exact(SemanticVersion version)

Parameters

version SemanticVersion
The exact version to match.

Returns

SemanticVersionRange A range that matches only the specified version.

public override int GetHashCode()

Returns

int

No documentation is available.

GreaterThan(SemanticVersion)

SemanticVersionRange.cs

Creates a range that matches versions greater than the specified version.

public static SemanticVersionRange GreaterThan(SemanticVersion version)

Parameters

version SemanticVersion
The minimum version (exclusive).

Returns

SemanticVersionRange A range that matches versions > the specified version.

GreaterThanOrEqual(SemanticVersion)

SemanticVersionRange.cs

Creates a range that matches versions greater than or equal to the specified version.

public static SemanticVersionRange GreaterThanOrEqual(SemanticVersion version)

Parameters

version SemanticVersion
The minimum version (inclusive).

Returns

SemanticVersionRange A range that matches versions >= the specified version.

LessThan(SemanticVersion)

SemanticVersionRange.cs

Creates a range that matches versions less than the specified version.

public static SemanticVersionRange LessThan(SemanticVersion version)

Parameters

version SemanticVersion
The maximum version (exclusive).

Returns

SemanticVersionRange A range that matches versions < the specified version.

LessThanOrEqual(SemanticVersion)

SemanticVersionRange.cs

Creates a range that matches versions less than or equal to the specified version.

public static SemanticVersionRange LessThanOrEqual(SemanticVersion version)

Parameters

version SemanticVersion
The maximum version (inclusive).

Returns

SemanticVersionRange A range that matches versions <= the specified version.

ParseNpm overloads

ParseNpm(ReadOnlySpan<char>)

SemanticVersionRange.cs

Parses a version range in npm format.

public static SemanticVersionRange ParseNpm(ReadOnlySpan<char> value)
Parameters
value ReadOnlySpan<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.cs

Parses a version range in npm format.

public static SemanticVersionRange ParseNpm(string value)
Parameters
value string
The string to parse.
Returns

SemanticVersionRange The parsed version range.

Exceptions
ArgumentNullException
value is 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 version
  • 1.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.cs

Parses a version range in NuGet format.

public static SemanticVersionRange ParseNuGet(ReadOnlySpan<char> value)
Parameters
value ReadOnlySpan<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.cs

Parses a version range in NuGet format.

public static SemanticVersionRange ParseNuGet(string value)
Parameters
value string
The string to parse.
Returns

SemanticVersionRange The parsed version range.

Exceptions
ArgumentNullException
value is 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.cs

Determines whether the specified version satisfies this range.

public bool Satisfies(SemanticVersion version)

Parameters

version SemanticVersion
The version to check.

Returns

bool true if the version satisfies the range; otherwise, false.

public override string ToString()

Returns

string

No documentation is available.

TryParseNpm overloads

TryParseNpm(ReadOnlySpan<char>, SemanticVersionRange?)

SemanticVersionRange.cs

Attempts to parse a version range in npm format.

public static bool TryParseNpm(ReadOnlySpan<char> value, out SemanticVersionRange? result)
Parameters
value ReadOnlySpan<char>
The span to parse.
result SemanticVersionRange?
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.cs

Attempts to parse a version range in npm format.

public static bool TryParseNpm(string? value, out SemanticVersionRange? result)
Parameters
value string?
The string to parse.
result SemanticVersionRange?
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.cs

Attempts to parse a version range in NuGet format.

public static bool TryParseNuGet(ReadOnlySpan<char> value, out SemanticVersionRange? result)
Parameters
value ReadOnlySpan<char>
The span to parse.
result SemanticVersionRange?
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.cs

Attempts to parse a version range in NuGet format.

public static bool TryParseNuGet(string? value, out SemanticVersionRange? result)
Parameters
value string?
The string to parse.
result SemanticVersionRange?
When this method returns, contains the parsed version range if the parse succeeded.
Returns

bool true if the parse succeeded; otherwise, false.

Operators

NameDescription
operator ==(SemanticVersionRange?, SemanticVersionRange?)
operator !=(SemanticVersionRange?, SemanticVersionRange?)

operator ==(SemanticVersionRange?, SemanticVersionRange?)

SemanticVersionRange.cs
public static bool operator ==(SemanticVersionRange? left, SemanticVersionRange? right)

No documentation is available.

operator !=(SemanticVersionRange?, SemanticVersionRange?)

SemanticVersionRange.cs
public static bool operator !=(SemanticVersionRange? left, SemanticVersionRange? right)

No documentation is available.