DayOfMonthSet Struct
Definition
Represents an immutable set of days of the month, 1 to 31.
public readonly struct DayOfMonthSet : ICalendarValueSet<DayOfMonthSet, int>, IEquatable<DayOfMonthSet>, IEqualityOperators<DayOfMonthSet, DayOfMonthSet, bool>, IBitwiseOperators<DayOfMonthSet, DayOfMonthSet, DayOfMonthSet>, ISpanParsable<DayOfMonthSet>, IParsable<DayOfMonthSet>, IUtf8SpanParsable<DayOfMonthSet>, ISpanFormattable, IFormattable, IUtf8SpanFormattable, IEnumerable<int>, IEnumerable
- Implements
- Inherited Members
- Extension Methods
Examples
var selected = new DayOfMonthSet(1, 15);
DayOfMonthSet parsed = DayOfMonthSet.Parse("1,15");
bool due = selected.Contains(DateTime.Today.Day);
bool same = selected == parsed; // true
Remarks
A DayOfMonthSet holds one bit per day, so it never allocates, and testing, adding or removing a day,
or combining two sets with |, &, ^ and ~, is a single bitwise operation.
Contains(int) answers false for any value outside 1 to 31 rather than throwing,
which keeps a membership test against Day to a comparison and a bit test.
The values are day numbers and do not depend on the length of a month: a set that contains 31 contains it whatever
the month, so a date in a 30-day month never matches it. Days counted back from the end of a month are a separate
question, which a caller answers by testing DateTime.DaysInMonth(year, month) - day + 1 against a second set.
The text form lists the days in ascending order, separated by commas, each run of two or more consecutive days
written as an inclusive range, so "1,15" selects the 1st and the 15th. The empty set is the empty string.
Parse(string) reads any list of values and ranges in that form, in any order, and
ToString() writes the canonical one. ToString(string?) also writes every day without
ranges, or a binary form with one character per day, and Parse(string) and
ParseExact(string, string) read them back.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?) writes the same text into a span
of characters, its other overload into a span of UTF-8 bytes, and the span and UTF-8 overloads of Parse,
TryParse, ParseExact and TryParseExact read it.
ToUInt64() and FromUInt64(ulong) expose the bits directly: bit n selects day
1 + n.
Constructors
DayOfMonthSet(params int[]?)
Initializes a new instance of the DayOfMonthSet struct that selects the specified days.
public DayOfMonthSet(params int[]? days)
Parameters
Remarks
A day may appear more than once, and the order is immaterial.
Exceptions
- ArgumentOutOfRangeException
Thrown when a value in
daysis less than 1 or greater than 31.
Properties
Count
Gets the number of days the set selects.
public int Count { get; }
Property Value
- int
A number from 0 to 31.
Methods
Contains(int)
Determines whether the set selects the specified day.
public bool Contains(int day)
Parameters
dayintThe day to test.
Returns
Equals(DayOfMonthSet)
Determines whether this set selects the same days as another.
public bool Equals(DayOfMonthSet other)
Parameters
otherDayOfMonthSetThe set to compare with.
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
GetEnumerator()
Returns an enumerator that yields the selected days in ascending order without allocating.
public DayOfMonthSet.Enumerator GetEnumerator()
Returns
- DayOfMonthSet.Enumerator
An enumerator over the selected days.
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.
Parse(ReadOnlySpan<byte>)
Converts UTF-8 text in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static DayOfMonthSet Parse(ReadOnlySpan<byte> utf8Text)
Parameters
utf8TextReadOnlySpan<byte>The UTF-8 text to convert.
Returns
- DayOfMonthSet
The set the text describes.
Remarks
The text is decoded and read as Parse(string) reads a string.
Exceptions
- FormatException
Thrown when
utf8Textis not valid UTF-8, or is neither the binary form nor a list of values and ranges from 1 to 31.
Parse(ReadOnlySpan<char>)
Converts a span of characters in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static DayOfMonthSet Parse(ReadOnlySpan<char> s)
Parameters
sReadOnlySpan<char>The characters to convert.
Returns
- DayOfMonthSet
The set the characters describe.
Remarks
The characters are read as Parse(string) reads a string.
Exceptions
- FormatException
Thrown when
sis neither the binary form nor a list of values and ranges from 1 to 31.
Parse(string)
Converts text in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static DayOfMonthSet Parse(string s)
Parameters
sstringThe text to convert, such as
"1,15".
Returns
- DayOfMonthSet
The set the text describes.
Remarks
A text of exactly 31 characters, each 0 or 1, is the binary form, day 1 first. Any other text is a
comma-separated list of days and inclusive ranges: whitespace around the list and around each value is ignored,
an empty or blank string is the empty set, values are unsigned decimal integers, a range a-b needs
a no greater than b, and values may repeat or overlap.
The binary test comes first and reads the text as given, so text made only of 0 and 1 is a list
only when it is not 31 characters long. ParseExact(string, string) with the "G" format
reads any text as a list.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when
sis neither the binary form nor a list of values and ranges from 1 to 31.
ToString()
Returns the canonical text form of the set: the selected days in ascending order, separated by commas, each run of two or more consecutive days written as an inclusive range.
public override string ToString()
Returns
- string
The text form, such as
"1,15,30-31", or the empty string for the empty set.
ToString(string?)
Returns the set as text in the specified format.
public string ToString(string? format)
Parameters
Returns
- string
The text.
Remarks
The formats are described at ToString(string?, IFormatProvider?).
Exceptions
- FormatException
Thrown when
formatis not a supported format.
ToString(string?, IFormatProvider?)
Returns the set as text in the specified format.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringThe format, or null or empty for the default.
formatProviderIFormatProviderIgnored; the text does not depend on culture.
Returns
- string
The text.
Remarks
The formats, whose letters are read in either case, are:
| Format | Text |
|---|---|
G, the default |
The days in ascending order, separated by commas, each run of two or more consecutive days as an inclusive
range: "1,15,30-31".
|
L | Every selected day listed, without ranges: "1,15,30,31". |
B, 0, 1, 01 |
Binary, one character per day from 1 to 31, 1 first, with 1 for a selected day:
"1000000000000010000000000000011".
|
Exceptions
- FormatException
Thrown when
formatis not a supported format.
ToUInt64()
Returns the set's bits, bit n selecting day 1 + n.
public ulong ToUInt64()
Returns
- ulong
The bits, with nothing set above bit 30.
TryFormat(Span<byte>, out int, ReadOnlySpan<char>, IFormatProvider?)
Attempts to write the set as UTF-8 text in the specified format into a span of bytes.
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Parameters
utf8DestinationSpan<byte>The span to write to.
bytesWrittenintWhen this method returns true, the number of bytes written; otherwise 0.
formatReadOnlySpan<char>The format, or an empty span for the default.
providerIFormatProviderIgnored; the text does not depend on culture.
Returns
- bool
true when the text fits in
utf8Destination; otherwise false, andutf8Destinationis left unchanged.
Remarks
The text is the one ToString(string?, IFormatProvider?) returns for the same format. It is ASCII, so each character is one byte.
Exceptions
- FormatException
Thrown when
formatis not a supported format.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Attempts to write the set as text in the specified format into a span of characters.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Parameters
destinationSpan<char>The span to write to.
charsWrittenintWhen this method returns true, the number of characters written; otherwise 0.
formatReadOnlySpan<char>The format, or an empty span for the default.
providerIFormatProviderIgnored; the text does not depend on culture.
Returns
Remarks
The text is the one ToString(string?, IFormatProvider?) returns for the same format.
Exceptions
- FormatException
Thrown when
formatis not a supported format.
TryParse(ReadOnlySpan<byte>, out DayOfMonthSet)
Attempts to convert UTF-8 text in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out DayOfMonthSet result)
Parameters
utf8TextReadOnlySpan<byte>The UTF-8 text to convert.
resultDayOfMonthSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
- bool
true when
utf8Textis valid UTF-8 for the binary form or a list of values and ranges from 1 to 31; otherwise false.
Remarks
The text is decoded and read as Parse(string) reads a string.
TryParse(ReadOnlySpan<char>, out DayOfMonthSet)
Attempts to convert a span of characters in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static bool TryParse(ReadOnlySpan<char> s, out DayOfMonthSet result)
Parameters
sReadOnlySpan<char>The characters to convert.
resultDayOfMonthSetWhen this method returns true, the set the characters describe; otherwise Empty.
Returns
Remarks
The characters are read as Parse(string) reads a string.
TryParse(string?, out DayOfMonthSet)
Attempts to convert text in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfMonthSet, detecting the form.
public static bool TryParse(string? s, out DayOfMonthSet result)
Parameters
sstringThe text to convert, such as
"1,15".resultDayOfMonthSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
Remarks
The text is read as Parse(string) reads it.
With(int)
Returns a set that also selects the specified day.
public DayOfMonthSet With(int day)
Parameters
dayintThe day to add.
Returns
- DayOfMonthSet
A set that selects
dayand every day this set selects.
Exceptions
- ArgumentOutOfRangeException
Thrown when
dayis less than 1 or greater than 31.
Without(int)
Returns a set that no longer selects the specified day.
public DayOfMonthSet Without(int day)
Parameters
dayintThe day to remove.
Returns
- DayOfMonthSet
A set that selects every day this set selects except
day.
Exceptions
- ArgumentOutOfRangeException
Thrown when
dayis less than 1 or greater than 31.
Explicit Interface Implementations
All
Gets the set that selects every day from 1 to 31.
static DayOfMonthSet All { get; }
Returns
Empty
Gets the set that selects no day.
static DayOfMonthSet Empty { get; }
Returns
FromUInt64(ulong)
Creates a set from its bits, bit n selecting day 1 + n.
static DayOfMonthSet FromUInt64(ulong bits)
Parameters
bitsulongThe bits.
Returns
- DayOfMonthSet
The set that
bitsdescribe.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bitssets a bit above bit 30, which selects no day.
ParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>)
Converts a span of characters in the specified format into a DayOfMonthSet.
static DayOfMonthSet ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format)
Parameters
sReadOnlySpan<char>The characters to convert.
formatReadOnlySpan<char>The format of
s, one of those ToString(string?) writes.
Returns
- DayOfMonthSet
The set the characters describe.
Remarks
The characters are read as ParseExact(string, string) reads a string.
Exceptions
- FormatException
Thrown when
formatis not a supported format, orsis not text in that format.
ParseExact(string, string)
Converts text in the specified format into a DayOfMonthSet.
static DayOfMonthSet ParseExact(string s, string format)
Parameters
sstringThe text to convert.
formatstringThe format of
s, one of those ToString(string?) writes.
Returns
- DayOfMonthSet
The set the text describes.
Remarks
"G" reads a list of values and ranges as Parse(string) reads one, "L" a list of
values without ranges, and "B", "0", "1" or "01" the binary form alone, 31
characters with no whitespace. The format letters are read in either case.
Exceptions
- ArgumentNullException
Thrown when
sorformatis null.- FormatException
Thrown when
formatis not a supported format, orsis not text in that format.
IEnumerable<int>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<int> IEnumerable<int>.GetEnumerator()
Returns
- IEnumerator<int>
An enumerator that can be used to iterate through the collection.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
IParsable<DayOfMonthSet>.Parse(string, IFormatProvider)
Parses a string into a value.
static DayOfMonthSet IParsable<DayOfMonthSet>.Parse(string s, IFormatProvider provider)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.
Returns
- DayOfMonthSet
The result of parsing
s.
Exceptions
- ArgumentNullException
sis null.- FormatException
sis not in the correct format.- OverflowException
sis not representable by DayOfMonthSet.
IParsable<DayOfMonthSet>.TryParse(string, IFormatProvider, out DayOfMonthSet)
Tries to parse a string into a value.
static bool IParsable<DayOfMonthSet>.TryParse(string s, IFormatProvider provider, out DayOfMonthSet result)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.resultDayOfMonthSetWhen this method returns, contains the result of successfully parsing
sor an undefined value on failure.
Returns
ISpanParsable<DayOfMonthSet>.Parse(ReadOnlySpan<char>, IFormatProvider)
Parses a span of characters into a value.
static DayOfMonthSet ISpanParsable<DayOfMonthSet>.Parse(ReadOnlySpan<char> s, IFormatProvider provider)
Parameters
sReadOnlySpan<char>The span of characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.
Returns
- DayOfMonthSet
The result of parsing
s.
Exceptions
- FormatException
sis not in the correct format.- OverflowException
sis not representable by DayOfMonthSet.
ISpanParsable<DayOfMonthSet>.TryParse(ReadOnlySpan<char>, IFormatProvider, out DayOfMonthSet)
Tries to parse a span of characters into a value.
static bool ISpanParsable<DayOfMonthSet>.TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out DayOfMonthSet result)
Parameters
sReadOnlySpan<char>The span of characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.resultDayOfMonthSetWhen this method returns, contains the result of successfully parsing
s, or an undefined value on failure.
Returns
IUtf8SpanParsable<DayOfMonthSet>.Parse(ReadOnlySpan<byte>, IFormatProvider)
Parses a span of UTF-8 characters into a value.
static DayOfMonthSet IUtf8SpanParsable<DayOfMonthSet>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider provider)
Parameters
utf8TextReadOnlySpan<byte>The span of UTF-8 characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
utf8Text.
Returns
- DayOfMonthSet
The result of parsing
utf8Text.
Exceptions
- FormatException
utf8Textis not in the correct format.- OverflowException
utf8Textis not representable by DayOfMonthSet.
IUtf8SpanParsable<DayOfMonthSet>.TryParse(ReadOnlySpan<byte>, IFormatProvider, out DayOfMonthSet)
Tries to parse a span of UTF-8 characters into a value.
static bool IUtf8SpanParsable<DayOfMonthSet>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider provider, out DayOfMonthSet result)
Parameters
utf8TextReadOnlySpan<byte>The span of UTF-8 characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
utf8Text.resultDayOfMonthSetOn return, contains the result of successfully parsing
utf8Textor an undefined value on failure.
Returns
TryParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>, out DayOfMonthSet)
Attempts to convert a span of characters in the specified format into a DayOfMonthSet.
static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, out DayOfMonthSet result)
Parameters
sReadOnlySpan<char>The characters to convert.
formatReadOnlySpan<char>The format of
s, one of those ToString(string?) writes.resultDayOfMonthSetWhen this method returns true, the set the characters describe; otherwise Empty.
Returns
Remarks
The characters are read as ParseExact(string, string) reads a string.
TryParseExact(string?, string?, out DayOfMonthSet)
Attempts to convert text in the specified format into a DayOfMonthSet.
static bool TryParseExact(string? s, string? format, out DayOfMonthSet result)
Parameters
sstringThe text to convert.
formatstringThe format of
s, one of those ToString(string?) writes.resultDayOfMonthSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
Remarks
The text is read as ParseExact(string, string) reads it.
operator &(DayOfMonthSet, DayOfMonthSet)
Returns the days that both sets select.
static DayOfMonthSet operator &(DayOfMonthSet left, DayOfMonthSet right)
Parameters
leftDayOfMonthSetThe first set.
rightDayOfMonthSetThe second set.
Returns
- DayOfMonthSet
The intersection of
leftandright.
operator |(DayOfMonthSet, DayOfMonthSet)
Returns the days that either set selects.
static DayOfMonthSet operator |(DayOfMonthSet left, DayOfMonthSet right)
Parameters
leftDayOfMonthSetThe first set.
rightDayOfMonthSetThe second set.
Returns
- DayOfMonthSet
The union of
leftandright.
operator ==(DayOfMonthSet, DayOfMonthSet)
Determines whether two sets select the same days.
static bool operator ==(DayOfMonthSet left, DayOfMonthSet right)
Parameters
leftDayOfMonthSetThe first set.
rightDayOfMonthSetThe second set.
Returns
operator ^(DayOfMonthSet, DayOfMonthSet)
Returns the days that exactly one of the sets selects.
static DayOfMonthSet operator ^(DayOfMonthSet left, DayOfMonthSet right)
Parameters
leftDayOfMonthSetThe first set.
rightDayOfMonthSetThe second set.
Returns
- DayOfMonthSet
The symmetric difference of
leftandright.
operator !=(DayOfMonthSet, DayOfMonthSet)
Determines whether two sets select different days.
static bool operator !=(DayOfMonthSet left, DayOfMonthSet right)
Parameters
leftDayOfMonthSetThe first set.
rightDayOfMonthSetThe second set.
Returns
operator ~(DayOfMonthSet)
Returns the days from 1 to 31 that the set does not select.
static DayOfMonthSet operator ~(DayOfMonthSet value)
Parameters
valueDayOfMonthSetThe set.
Returns
- DayOfMonthSet
The complement of
valuewithin 1 to 31.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |