DayOfWeekSet Struct
Definition
Represents an immutable set of days of the week.
public readonly struct DayOfWeekSet : ICalendarValueSet<DayOfWeekSet, DayOfWeek>, IEquatable<DayOfWeekSet>, IEqualityOperators<DayOfWeekSet, DayOfWeekSet, bool>, IBitwiseOperators<DayOfWeekSet, DayOfWeekSet, DayOfWeekSet>, ISpanParsable<DayOfWeekSet>, IParsable<DayOfWeekSet>, IUtf8SpanParsable<DayOfWeekSet>, ISpanFormattable, IFormattable, IUtf8SpanFormattable, IEnumerable<DayOfWeek>, IEnumerable
- Implements
- Inherited Members
- Extension Methods
Examples
DayOfWeekSet fourDayWeek = DayOfWeekSet.Weekdays.Without(DayOfWeek.Friday);
bool working = fourDayWeek.Contains(DateTime.Today.DayOfWeek);
string text = fourDayWeek.ToString(); // "_MTWT__"
bool same = DayOfWeekSet.Parse(text) == fourDayWeek; // true
Remarks
A DayOfWeekSet 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(DayOfWeek) answers false for a value outside the seven days rather than
throwing, which keeps a membership test against DayOfWeek to a comparison and a bit test.
The text form is a seven-character mask with one character per day, the day's letter when it is selected and
_ when it is not, Sunday first: "_MTWTF_" selects Monday to Friday. ToString(string?)
can also write the mask Monday first, with another placeholder, or in binary ("0111110"), and
Parse(string) reads any of those forms.
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
(DayOfWeek)n, so Sunday is bit 0 and Saturday bit 6.
The presets are Weekdays, Weekend, and one for each of the seven named working weeks of WorkingDaysOfWeek. Its two other values have no preset: AllDays corresponds to All, and Custom has no set.
Constructors
DayOfWeekSet(params DayOfWeek[]?)
Initializes a new instance of the DayOfWeekSet struct that selects the specified days.
public DayOfWeekSet(params DayOfWeek[]? days)
Parameters
Remarks
A day may appear more than once, and the order is immaterial.
Exceptions
- ArgumentOutOfRangeException
Thrown when a value in
daysis not one of the seven DayOfWeek values.
Properties
Count
Gets the number of days the set selects.
public int Count { get; }
Property Value
- int
A number from 0 to 7.
MondayToFriday
Gets the working week of Monday to Friday.
public static DayOfWeekSet MondayToFriday { get; }
Property Value
Remarks
Equal to Weekdays, and corresponds to MondayToFriday.
MondayToSaturday
Gets the working week of Monday to Saturday.
public static DayOfWeekSet MondayToSaturday { get; }
Property Value
Remarks
Corresponds to MondayToSaturday.
MondayToThursdayAndSaturday
Gets the working week of Monday to Thursday and Saturday.
public static DayOfWeekSet MondayToThursdayAndSaturday { get; }
Property Value
Remarks
Corresponds to MondayToThursdayAndSaturday.
SaturdayToThursday
Gets the working week of Saturday to Thursday.
public static DayOfWeekSet SaturdayToThursday { get; }
Property Value
Remarks
Corresponds to SaturdayToThursday.
SaturdayToWednesday
Gets the working week of Saturday to Wednesday.
public static DayOfWeekSet SaturdayToWednesday { get; }
Property Value
Remarks
Corresponds to SaturdayToWednesday.
SundayToFriday
Gets the working week of Sunday to Friday.
public static DayOfWeekSet SundayToFriday { get; }
Property Value
Remarks
Corresponds to SundayToFriday.
SundayToThursday
Gets the working week of Sunday to Thursday.
public static DayOfWeekSet SundayToThursday { get; }
Property Value
Remarks
Corresponds to SundayToThursday.
Weekdays
Gets the set of Monday to Friday.
public static DayOfWeekSet Weekdays { get; }
Property Value
Remarks
Equal to MondayToFriday, and corresponds to MondayToFriday.
Weekend
Gets the set of Saturday and Sunday.
public static DayOfWeekSet Weekend { get; }
Property Value
Methods
Contains(DayOfWeek)
Determines whether the set selects the specified day.
public bool Contains(DayOfWeek day)
Parameters
dayDayOfWeekThe day to test.
Returns
Equals(DayOfWeekSet)
Determines whether this set selects the same days as another.
public bool Equals(DayOfWeekSet other)
Parameters
otherDayOfWeekSetThe 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 DayOfWeek order, Sunday first, without allocating.
public DayOfWeekSet.Enumerator GetEnumerator()
Returns
- DayOfWeekSet.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 DayOfWeekSet, detecting the form.
public static DayOfWeekSet Parse(ReadOnlySpan<byte> utf8Text)
Parameters
utf8TextReadOnlySpan<byte>The UTF-8 text to convert.
Returns
- DayOfWeekSet
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 not a seven-character mask.
Parse(ReadOnlySpan<char>)
Converts a span of characters holding a seven-character mask into a DayOfWeekSet, detecting the mask's form.
public static DayOfWeekSet Parse(ReadOnlySpan<char> s)
Parameters
sReadOnlySpan<char>The characters to convert.
Returns
- DayOfWeekSet
The set the characters describe.
Remarks
The characters are read as Parse(string) reads a string.
Exceptions
- FormatException
Thrown when
sis not a seven-character mask.
Parse(string)
Converts a seven-character mask into a DayOfWeekSet, detecting the mask's form.
public static DayOfWeekSet Parse(string s)
Parameters
sstringThe text to convert, such as
"MTWTF"or"0111110".
Returns
- DayOfWeekSet
The set the text describes.
Remarks
The text has one character per day. A day's letter, in either case, selects the day, and _, -,
* or a space leaves it out; one placeholder is used throughout. The letters show whether the mask starts
on Sunday or on Monday, the first letter that fits only one of the two orders deciding. A mask that begins with
0 or 1 is the binary form, Sunday first, with 1 for a selected day.
One mask fits both orders: "______S", whose only letter is a final S, is Saturday Sunday first and
Sunday Monday first. It is read Sunday first, as ToString() writes it; read Monday-first text
with ParseExact(string, string) and the "M" format.
Anything else, an abbreviated list of days such as "MF" included, is not a mask.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when
sis not a seven-character mask.
ToString()
Returns the set as a seven-character mask, Sunday first, with each day's letter when it is selected and _
when it is not.
public override string ToString()
Returns
- string
The mask, such as
"MTWTF"for Monday to Friday.
ToString(string?)
Returns the set as a seven-character mask in the specified format.
public string ToString(string? format)
Parameters
Returns
- string
The mask.
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 a seven-character mask in the specified format.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringThe format, or null or empty for the default.
formatProviderIFormatProviderIgnored; the mask does not depend on culture.
Returns
- string
The mask.
Remarks
The formats, whose letters are read in either case, are:
| Format | Mask |
|---|---|
S or G, the default | Sunday first, with _ for a day not selected: "_MTWTF_". |
M | Monday first, with _ for a day not selected: "MTWTF__". |
E, U, D, A |
Sunday first, with a space, _, - or * respectively for a day not selected.
|
S or M followed by E, U, D or A | That order, with that placeholder: "MD" writes "MTWTF--". |
B, 0, 1, 01 | Binary, Sunday first, with 1 for a selected day: "0111110". |
Exceptions
- FormatException
Thrown when
formatis not a supported format.
ToUInt64()
Returns the set's bits, bit n selecting (DayOfWeek)n.
public ulong ToUInt64()
Returns
- ulong
The bits, with nothing set above bit 6.
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 DayOfWeekSet)
Attempts to convert UTF-8 text in one of the forms ToString(string?, IFormatProvider?) writes into a DayOfWeekSet, detecting the form.
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out DayOfWeekSet result)
Parameters
utf8TextReadOnlySpan<byte>The UTF-8 text to convert.
resultDayOfWeekSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
Remarks
The text is decoded and read as Parse(string) reads a string.
TryParse(ReadOnlySpan<char>, out DayOfWeekSet)
Attempts to convert a span of characters holding a seven-character mask into a DayOfWeekSet, detecting the mask's form.
public static bool TryParse(ReadOnlySpan<char> s, out DayOfWeekSet result)
Parameters
sReadOnlySpan<char>The characters to convert.
resultDayOfWeekSetWhen 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 DayOfWeekSet)
Attempts to convert a seven-character mask into a DayOfWeekSet, detecting the mask's form.
public static bool TryParse(string? s, out DayOfWeekSet result)
Parameters
sstringThe text to convert.
resultDayOfWeekSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
Remarks
The text is read as Parse(string) reads it.
With(DayOfWeek)
Returns a set that also selects the specified day.
public DayOfWeekSet With(DayOfWeek day)
Parameters
dayDayOfWeekThe day to add.
Returns
- DayOfWeekSet
A set that selects
dayand every day this set selects.
Exceptions
- ArgumentOutOfRangeException
Thrown when
dayis not one of the seven DayOfWeek values.
Without(DayOfWeek)
Returns a set that no longer selects the specified day.
public DayOfWeekSet Without(DayOfWeek day)
Parameters
dayDayOfWeekThe day to remove.
Returns
- DayOfWeekSet
A set that selects every day this set selects except
day.
Exceptions
- ArgumentOutOfRangeException
Thrown when
dayis not one of the seven DayOfWeek values.
Explicit Interface Implementations
All
Gets the set that selects every day of the week.
static DayOfWeekSet All { get; }
Returns
Remarks
Corresponds to AllDays.
Empty
Gets the set that selects no day.
static DayOfWeekSet Empty { get; }
Returns
FromUInt64(ulong)
Creates a set from its bits, bit n selecting (DayOfWeek)n.
static DayOfWeekSet FromUInt64(ulong bits)
Parameters
bitsulongThe bits.
Returns
- DayOfWeekSet
The set that
bitsdescribe.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bitssets a bit above bit 6, which selects no day.
ParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>)
Converts a span of characters holding a seven-character mask in the specified format into a DayOfWeekSet.
static DayOfWeekSet 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
- DayOfWeekSet
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 a mask in that format.
ParseExact(string, string)
Converts a seven-character mask in the specified format into a DayOfWeekSet.
static DayOfWeekSet ParseExact(string s, string format)
Parameters
sstringThe text to convert.
formatstringThe format of
s, one of those ToString(string?) writes.
Returns
- DayOfWeekSet
The set the text describes.
Remarks
The format fixes the order of the days, and the placeholder for a day not selected when it names one: with
"S" or "M" the placeholder is any of _, -, * or a space, used throughout. The
letters of the days and of the format are read in either case.
Exceptions
- ArgumentNullException
Thrown when
sorformatis null.- FormatException
Thrown when
formatis not a supported format, orsis not a mask in that format.
IEnumerable<DayOfWeek>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<DayOfWeek> IEnumerable<DayOfWeek>.GetEnumerator()
Returns
- IEnumerator<DayOfWeek>
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<DayOfWeekSet>.Parse(string, IFormatProvider)
Parses a string into a value.
static DayOfWeekSet IParsable<DayOfWeekSet>.Parse(string s, IFormatProvider provider)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.
Returns
- DayOfWeekSet
The result of parsing
s.
Exceptions
- ArgumentNullException
sis null.- FormatException
sis not in the correct format.- OverflowException
sis not representable by DayOfWeekSet.
IParsable<DayOfWeekSet>.TryParse(string, IFormatProvider, out DayOfWeekSet)
Tries to parse a string into a value.
static bool IParsable<DayOfWeekSet>.TryParse(string s, IFormatProvider provider, out DayOfWeekSet result)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.resultDayOfWeekSetWhen this method returns, contains the result of successfully parsing
sor an undefined value on failure.
Returns
ISpanParsable<DayOfWeekSet>.Parse(ReadOnlySpan<char>, IFormatProvider)
Parses a span of characters into a value.
static DayOfWeekSet ISpanParsable<DayOfWeekSet>.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
- DayOfWeekSet
The result of parsing
s.
Exceptions
- FormatException
sis not in the correct format.- OverflowException
sis not representable by DayOfWeekSet.
ISpanParsable<DayOfWeekSet>.TryParse(ReadOnlySpan<char>, IFormatProvider, out DayOfWeekSet)
Tries to parse a span of characters into a value.
static bool ISpanParsable<DayOfWeekSet>.TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out DayOfWeekSet result)
Parameters
sReadOnlySpan<char>The span of characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.resultDayOfWeekSetWhen this method returns, contains the result of successfully parsing
s, or an undefined value on failure.
Returns
IUtf8SpanParsable<DayOfWeekSet>.Parse(ReadOnlySpan<byte>, IFormatProvider)
Parses a span of UTF-8 characters into a value.
static DayOfWeekSet IUtf8SpanParsable<DayOfWeekSet>.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
- DayOfWeekSet
The result of parsing
utf8Text.
Exceptions
- FormatException
utf8Textis not in the correct format.- OverflowException
utf8Textis not representable by DayOfWeekSet.
IUtf8SpanParsable<DayOfWeekSet>.TryParse(ReadOnlySpan<byte>, IFormatProvider, out DayOfWeekSet)
Tries to parse a span of UTF-8 characters into a value.
static bool IUtf8SpanParsable<DayOfWeekSet>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider provider, out DayOfWeekSet result)
Parameters
utf8TextReadOnlySpan<byte>The span of UTF-8 characters to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
utf8Text.resultDayOfWeekSetOn return, contains the result of successfully parsing
utf8Textor an undefined value on failure.
Returns
TryParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>, out DayOfWeekSet)
Attempts to convert a span of characters holding a seven-character mask in the specified format into a DayOfWeekSet.
static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, out DayOfWeekSet result)
Parameters
sReadOnlySpan<char>The characters to convert.
formatReadOnlySpan<char>The format of
s, one of those ToString(string?) writes.resultDayOfWeekSetWhen 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 DayOfWeekSet)
Attempts to convert a seven-character mask in the specified format into a DayOfWeekSet.
static bool TryParseExact(string? s, string? format, out DayOfWeekSet result)
Parameters
sstringThe text to convert.
formatstringThe format of
s, one of those ToString(string?) writes.resultDayOfWeekSetWhen this method returns true, the set the text describes; otherwise Empty.
Returns
Remarks
The text is read as ParseExact(string, string) reads it.
operator &(DayOfWeekSet, DayOfWeekSet)
Returns the days that both sets select.
static DayOfWeekSet operator &(DayOfWeekSet left, DayOfWeekSet right)
Parameters
leftDayOfWeekSetThe first set.
rightDayOfWeekSetThe second set.
Returns
- DayOfWeekSet
The intersection of
leftandright.
operator |(DayOfWeekSet, DayOfWeekSet)
Returns the days that either set selects.
static DayOfWeekSet operator |(DayOfWeekSet left, DayOfWeekSet right)
Parameters
leftDayOfWeekSetThe first set.
rightDayOfWeekSetThe second set.
Returns
- DayOfWeekSet
The union of
leftandright.
operator ==(DayOfWeekSet, DayOfWeekSet)
Determines whether two sets select the same days.
static bool operator ==(DayOfWeekSet left, DayOfWeekSet right)
Parameters
leftDayOfWeekSetThe first set.
rightDayOfWeekSetThe second set.
Returns
operator ^(DayOfWeekSet, DayOfWeekSet)
Returns the days that exactly one of the sets selects.
static DayOfWeekSet operator ^(DayOfWeekSet left, DayOfWeekSet right)
Parameters
leftDayOfWeekSetThe first set.
rightDayOfWeekSetThe second set.
Returns
- DayOfWeekSet
The symmetric difference of
leftandright.
operator !=(DayOfWeekSet, DayOfWeekSet)
Determines whether two sets select different days.
static bool operator !=(DayOfWeekSet left, DayOfWeekSet right)
Parameters
leftDayOfWeekSetThe first set.
rightDayOfWeekSetThe second set.
Returns
operator ~(DayOfWeekSet)
Returns the days of the week that the set does not select.
static DayOfWeekSet operator ~(DayOfWeekSet value)
Parameters
valueDayOfWeekSetThe set.
Returns
- DayOfWeekSet
The complement of
valuewithin the seven days.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |