Range<T> Struct

Represents a range of comparable values with support for various inclusion/exclusion boundary checks.

public readonly struct Range<T> : IEquatable<Range<T>> where T : IComparable<T>

Type parameters

T

Example

var range = new Range<int>(1, 10);
Assert.True(range.IsInRangeInclusive(5));
Assert.False(range.IsInRangeInclusive(15));
Implements

Constructors

NameDescription
Range(T, T)Initializes a new instance of the Range<T> struct.

Range(T, T)

Range.cs

Initializes a new instance of the Range<T> struct.

public Range(T from, T to)

Parameters

from T
to T

Exceptions

ArgumentException
from is greater than to.

Properties

NameDescription
FromGets the start value of the range.
ToGets the end value of the range.

Gets the start value of the range.

public T From { get; }

Property value

T

Gets the end value of the range.

public T To { get; }

Property value

T

Methods

NameDescription
Equals(Range<T>)
Equals(object?)
GetHashCode()
Intersect(Range<T>)Returns the values that are in both this range and the specified range, inclusive of boundaries.
IsInRangeExclusive(Range<T>)Determines whether the specified range is fully contained within this range, exclusive of boundaries.
IsInRangeExclusive(T)Determines whether the specified value is within this range, exclusive of boundaries.
IsInRangeInclusive(Range<T>)Determines whether the specified range is fully contained within this range, inclusive of boundaries.
IsInRangeInclusive(T)Determines whether the specified value is within this range, inclusive of boundaries.
IsInRangeLowerInclusive(Range<T>)Determines whether the specified range is fully contained within this range, inclusive of the lower bound and exclusive of the upper bound.
IsInRangeLowerInclusive(T)Determines whether the specified value is within this range, inclusive of the lower bound and exclusive of the upper bound.
IsInRangeUpperInclusive(Range<T>)Determines whether the specified range is fully contained within this range, exclusive of the lower bound and inclusive of the upper bound.
IsInRangeUpperInclusive(T)Determines whether the specified value is within this range, exclusive of the lower bound and inclusive of the upper bound.
Overlaps(Range<T>)Determines whether this range and the specified range have at least one value in common, inclusive of boundaries.
ToString()

Equals overloads

Equals(Range<T>)

Range.cs
public bool Equals(Range<T> other)
Parameters
other Range<T>
Returns

bool

No documentation is available.

Equals(object?)

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

bool

No documentation is available.

GetHashCode()

Range.cs
public override int GetHashCode()

Returns

int

No documentation is available.

Intersect(Range<T>)

Range.cs

Returns the values that are in both this range and the specified range, inclusive of boundaries.

public Range<T>? Intersect(Range<T> range)

Parameters

range Range<T>

Returns

Range<T>? The intersection of both ranges, or null when they do not overlap.

Remarks

A null bound is open: it is lower or greater than any value.

IsInRangeExclusive overloads

IsInRangeExclusive(Range<T>)

Range.cs

Determines whether the specified range is fully contained within this range, exclusive of boundaries.

public bool IsInRangeExclusive(Range<T> range)
Parameters
range Range<T>
Returns

bool

IsInRangeExclusive(T)

Range.cs

Determines whether the specified value is within this range, exclusive of boundaries.

public bool IsInRangeExclusive(T value)
Parameters
value T
Returns

bool

IsInRangeInclusive overloads

IsInRangeInclusive(Range<T>)

Range.cs

Determines whether the specified range is fully contained within this range, inclusive of boundaries.

public bool IsInRangeInclusive(Range<T> range)
Parameters
range Range<T>
Returns

bool

IsInRangeInclusive(T)

Range.cs

Determines whether the specified value is within this range, inclusive of boundaries.

public bool IsInRangeInclusive(T value)
Parameters
value T
Returns

bool

IsInRangeLowerInclusive overloads

IsInRangeLowerInclusive(Range<T>)

Range.cs

Determines whether the specified range is fully contained within this range, inclusive of the lower bound and exclusive of the upper bound.

public bool IsInRangeLowerInclusive(Range<T> range)
Parameters
range Range<T>
Returns

bool

IsInRangeLowerInclusive(T)

Range.cs

Determines whether the specified value is within this range, inclusive of the lower bound and exclusive of the upper bound.

public bool IsInRangeLowerInclusive(T value)
Parameters
value T
Returns

bool

IsInRangeUpperInclusive overloads

IsInRangeUpperInclusive(Range<T>)

Range.cs

Determines whether the specified range is fully contained within this range, exclusive of the lower bound and inclusive of the upper bound.

public bool IsInRangeUpperInclusive(Range<T> range)
Parameters
range Range<T>
Returns

bool

IsInRangeUpperInclusive(T)

Range.cs

Determines whether the specified value is within this range, exclusive of the lower bound and inclusive of the upper bound.

public bool IsInRangeUpperInclusive(T value)
Parameters
value T
Returns

bool

Overlaps(Range<T>)

Range.cs

Determines whether this range and the specified range have at least one value in common, inclusive of boundaries.

public bool Overlaps(Range<T> range)

Parameters

range Range<T>

Returns

bool

Remarks

A null bound is open: it is lower or greater than any value.

ToString()

Range.cs
public override string? ToString()

Returns

string?

No documentation is available.

Operators

NameDescription
operator ==(Range<T>, Range<T>)
operator !=(Range<T>, Range<T>)

operator ==(Range<T>, Range<T>)

Range.cs
public static bool operator ==(Range<T> left, Range<T> right)

Parameters

left Range<T>
right Range<T>

Returns

bool

No documentation is available.

operator !=(Range<T>, Range<T>)

Range.cs
public static bool operator !=(Range<T> left, Range<T> right)

Parameters

left Range<T>
right Range<T>

Returns

bool

No documentation is available.