Range<T>

References
public struct Range<T> : IEquatable<Range<T>>
Represents an immutable, inclusive start/end range of IComparable<T> values
T where struct, IComparable<T>

Constructors

Range

#
public Range(T start, T end)
Initializes a new instance of the Range<T> struct.
start (T)Start of the range (inclusive).
end (T)End of the range (inclusive).

Range

#
public Range(T value)
Initializes a new instance of the Range<T> struct with a value for both the start and end.

Properties

End

#
public T End { get; }
Gets the end value of the range.

Start

#
public T Start { get; }
Gets the start value of the range

Methods

Contains

#
public bool Contains(T value)
Determines if the specified value is between or equal to the Start and End of this range.

Equals

#
public override bool Equals(object obj)
Determines whether the specified object is equal to the current Range<T>.
obj (object)The object to compare with the current Range<T>.
true if the specified object is equal to the current Range<T>; otherwise, false.
ValueType.Equals

Equals

#
public bool Equals(Range<T> other)
Determines whether the specified other range is equal to the current Range<T>.
other (Range<T>)The Range<T> to compare with the current Range<T>.
true if the specified Range<T> is equal to the current Range<T>; otherwise, false.

GetHashCode

#
public override int GetHashCode()
Serves as a hash function for a Range<T> object.
A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.

Intersect

#
public Range<T>? Intersect(Range<T> range)
Gets the intersection of this instance and the specified range.
range (Range<T>)Range to intersect with.
A new instance of a range that is the intersection of this instance and the specified range, or null if they do not intersect.

Intersects

#
public bool Intersects(Range<T> range)
Determines if the specified range intersects (overlaps) this instance.
range (Range<T>)Range to check for intersection.
true if the range intersects this instance, false otherwise.

Iterate

#
public IEnumerable<T> Iterate(Func<T, T> increment)
Iterates the range between the start and end values.
Remarks: This can be used to return an enumerable that iterates between the start and end of the range given the specified increment function.
increment (Func<T, T>)Delegate to increment the value for each iteration of the enumerable.
// iterate over an int range
var intRange = new Range<int>(1, 200);
foreach (var item in intRange.Iterate(i => i + 1))
{
	// logic
}

// iterate over a date range by minute
var dateRange = new Range<DateTime>(DateTime.Today, DateTime.Today.AddDays(2));
foreach (var item in dateRange.Iterate(i => i.AddMinutes(1)))
{
	// logic
}

ToString

#
public override string ToString()
Returns a string that represents the current Range<T>.

Touches

#
public bool Touches(Range<T> range, Func<T, T> increment)
Determines if the specified range touches (but doesn't intersect) this instance.
Remarks: This can be used to determine if one range comes after or before another range, given the specified increment function. The increment function is used as this class does not assume how to increment each value, e.g. for a DateTime value, you can increment by day, minute, second, etc.
range (Range<T>)Range to check if it touches this range.
increment (Func<T, T>)Delegate to increment the value for checking if the ranges touch.
true if the ranges touch, false otherwise.

Union

#
public Range<T>? Union(Range<T> range, Func<T, T> increment)
Gets the union of this instance and the specified range, including touching ranges.
Remarks: This is similar to Union(Range<T>), however this handles when the two ranges are touching. The increment delegate is used to determine if the ranges are touching by incrementing the ends of the ranges and comparing that value to the start of the other range.
range (Range<T>)Range to union with.
increment (Func<T, T>)Delegate to increment the value for checking if the ranges touch.
The union of this instance and the specified range, or null if they are neither intersecting or touching.

Union

#
public Range<T>? Union(Range<T> range)
Gets the union of this instance and an intersecting range.
Remarks: This is similar to Union(Range<T>, Func<T, T>), however this only handles when the two ranges are intersecting. To union two ranges that touch, use the Union(Range<T>, Func<T, T>) method instead.
range (Range<T>)Range to union with.
The union of this instance and the specified range, or null if they are not intersecting.

WithEnd

#
public Range<T> WithEnd(T end)
Creates a copy of the current range with a different end value.
end (T)End of the new range.
A new instance of the range with the specified end value.

WithStart

#
public Range<T> WithStart(T start)
Creates a copy of the current range with a different start value.
start (T)Start of the new range.
A new instance of the range with the specified start value.

Operators

OperationResultDescription
Range<T>==Range<T>boolOperator to compare two ranges for equality
Range<T>!=Range<T>boolOperator to compare two ranges for inequality.