Table of Contents

Range<T> Struct

Definition

Namespace
Bodu.Collections.Generic
Assembly
Bodu.Collections.dll
Package
Bodu.Collections 1.0.0
Source
Range{T}.cs

Represents a half-open range, where StartInclusive is included and EndExclusive is excluded.

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

Type Parameters

T

The comparable endpoint type.

Implements
Inherited Members
Extension Methods

Remarks

Range<T> is an immutable value type used as the element of RangeSet<T> and as a shared building block for the RangeDictionary<TKey, TValue> family. The constructor validates that the start endpoint is strictly less than the end endpoint and rejects degenerate or inverted ranges with ArgumentException.

Half-open semantics - [start, end) - match the convention used by .NET span slicing, Range, and most database BETWEEN alternatives. They allow adjacent ranges ( [0, 5) followed by [5, 10)) to abut without overlapping, which is the property that keeps RangeSet<T> and RangeDictionary<TKey, TValue> internally consistent.

Equality is structural: two Range<T> values are equal when both endpoints compare equal under the default endpoint comparer. Range<T> is suitable as a dictionary key when T is itself a well-behaved value type.

Constructors

Range(T, T)

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

public Range(T startInclusive, T endExclusive)

Parameters

startInclusive T

The inclusive start of the range.

endExclusive T

The exclusive end of the range.

Exceptions

ArgumentNullException

startInclusive or endExclusive is null.

ArgumentException

startInclusive is greater than or equal to endExclusive.

Properties

EndExclusive

Gets the exclusive end of the range.

public T EndExclusive { get; }

Property Value

T

The exclusive upper bound of the range.

StartInclusive

Gets the inclusive start of the range.

public T StartInclusive { get; }

Property Value

T

The inclusive lower bound of the range.

Methods

Contains(T)

Determines whether the specified value falls inside this range.

public bool Contains(T value)

Parameters

value T

The value to test. Must not be null.

Returns

bool

true if value is inside the range; otherwise, false.

Exceptions

ArgumentNullException

value is null.

Equals(Range<T>)

Determines whether two ranges have identical endpoints.

public bool Equals(Range<T> other)

Parameters

other Range<T>

The range to compare with this instance.

Returns

bool

true if the ranges share the same start and end; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string

The fully qualified type name.

Operators

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

Returns a value indicating whether two ranges have identical endpoints.

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

Parameters

left Range<T>

The left operand.

right Range<T>

The right operand.

Returns

bool

true if the operands are equal; otherwise, false.

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

Returns a value indicating whether two ranges have differing endpoints.

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

Parameters

left Range<T>

The left operand.

right Range<T>

The right operand.

Returns

bool

true if the operands are not equal; otherwise, false.

Applies to

ProductVersions
.NET8, 10