DateRange
Struct sealedpublic
Represents an inclusive date range defined by a start and end boundary, with helpers for containment checks, overlap detection, and duration calculation.
Definition
Section titled Definitionpublic sealed struct DateRange : System.IEquatable<SampleApi.DateRange> IEquatable<DateRange>
Inheritance
Section titled Inheritance IEquatable<DateRange>
classDiagram
direction TB
class DateRange {
<<struct>>
}
class IEquatable~DateRange~ {
<<interface>>
}
IEquatable~DateRange~ <|.. DateRangeRemarks
Section titled RemarksDateRange is a
readonly struct that implements IEquatable`1 for efficient, allocation-free value comparisons. The constructor enforces the invariant that Start must be on or before End, throwing ArgumentException otherwise. Common use cases include filtering orders by date, defining subscription periods, and computing business-day durations. The DateTimeOffset) and DateRange) methods enable calendar-style range queries.Constructors1
Section titled ConstructorsConstructor Section titled Constructor DateRange.DateRange( DateTimeOffset start, DateTimeOffset end)Initializes a new instance of the DateRange struct.
Parameters
start DateTimeOffset The inclusive start date of the range.
end DateTimeOffset The inclusive end date of the range.
Exceptions
ArgumentException Thrown when
start is after end. Properties3
Section titled PropertiesDuration Section titled Duration TimeSpan TimeSpan DateRange.DurationGets the duration of time between Start and End.
DateTimeOffset DateTimeOffset DateRange.EndGets the inclusive end date of the range.
Start Section titled Start DateTimeOffset DateTimeOffset DateRange.StartGets the inclusive start date of the range.
Methods8
Section titled MethodsContains Section titled Contains bool bool DateRange.Contains( DateTimeOffset date)Determines whether the specified date falls within this range.
Parameters
date DateTimeOffset The date to check.
Returns
bool true if date is within the range; otherwise, false. Equals Section titled Equals bool bool DateRange.Equals( DateRange other)Parameters
other DateRange bool bool DateRange.Equals( object? obj)Parameters
obj object? int int DateRange.GetHashCode()bool bool DateRange.operator ==( DateRange left, DateRange right)Determines whether two DateRange instances are equal.
bool bool DateRange.operator !=( DateRange left, DateRange right)Determines whether two DateRange instances are not equal.
Overlaps Section titled Overlaps bool bool DateRange.Overlaps( DateRange other)Determines whether this range overlaps with another range.
Parameters
other DateRange The other date range to test.
Returns
bool true if the ranges overlap; otherwise, false. string string DateRange.ToString()Returns a human-readable representation of the date range.
Examples
Section titled ExamplesCreating a date range and testing containment:
var q1 = new DateRange( new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero), new DateTimeOffset(2026, 3, 31, 23, 59, 59, TimeSpan.Zero));
bool inRange = q1.Contains(DateTimeOffset.UtcNow);Console.WriteLine($"Q1 duration: {q1.Duration.TotalDays} days");Checking whether two ranges overlap:
var feb = new DateRange( new DateTimeOffset(2026, 2, 1, 0, 0, 0, TimeSpan.Zero), new DateTimeOffset(2026, 2, 28, 23, 59, 59, TimeSpan.Zero));bool overlaps = q1.Overlaps(feb); // true