Type parameters:
| T | where struct, IComparable<T> |
Constructors
Range
#public Range(T start, T end)
Initializes a new instance of the Range<T> struct.
Parameters:
| start (T) | Start of the range (inclusive). |
| end (T) | End of the range (inclusive). |
Properties
Methods
Equals
#Overrides: ValueType.Equals
GetHashCode
#Serves as a hash function for a Range<T> object.
Returns: A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.
Overrides: ValueType.GetHashCode
Intersect
#Gets the intersection of this instance and the specified
range.Parameters:
| range (Range<T>) | Range to intersect with. |
Returns: 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
#Determines if the specified
range intersects (overlaps) this instance.Parameters:
| range (Range<T>) | Range to check for intersection. |
Returns:
true if the range intersects this instance, false otherwise.Iterate
#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.Parameters:
| increment (Func<T, T>) | Delegate to increment the value for each iteration of the enumerable. |
Example:
// 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
}Touches
#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.Parameters:
| 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. |
Returns:
true if the ranges touch, false otherwise.Union
#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.Parameters:
| range (Range<T>) | Range to union with. |
| increment (Func<T, T>) | Delegate to increment the value for checking if the ranges touch. |
Returns: The union of this instance and the specified range, or null if they are neither intersecting or touching.
Union
#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.
Parameters:
| range (Range<T>) | Range to union with. |
Returns: The union of this instance and the specified range, or null if they are not intersecting.
WithEnd
#Creates a copy of the current range with a different end value.
Parameters:
| end (T) | End of the new range. |
Returns: A new instance of the range with the specified end value.
WithStart
#Creates a copy of the current range with a different start value.
Parameters:
| start (T) | Start of the new range. |
Returns: A new instance of the range with the specified start value.