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
TThe comparable endpoint type.
- Implements
-
IEquatable<Range<T>>
- 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
startInclusiveTThe inclusive start of the range.
endExclusiveTThe exclusive end of the range.
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
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
valueTThe value to test. Must not be null.
Returns
Exceptions
- ArgumentNullException
valueis null.
Equals(Range<T>)
Determines whether two ranges have identical endpoints.
public bool Equals(Range<T> other)
Parameters
otherRange<T>The range to compare with this instance.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
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
Returns
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
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |