WeekPattern Struct
Definition
Represents an immutable set of selected days in a standard seven-day week.
public readonly struct WeekPattern : IEnumerable<DayOfWeek>, IEnumerable, IFormattable, IComparable<WeekPattern>, IComparable<byte>, IComparable, IEquatable<WeekPattern>, IEquatable<byte>, ISerializable
- Implements
- Inherited Members
- Extension Methods
Remarks
WeekPattern provides a compact bitmask representation of selected days, supporting non-destructive composition, parsing, formatting, comparison, and enumeration.
Because WeekPattern is a value type, all operations that change the selection return a new instance rather than modifying the receiver. Use With(DayOfWeek) and Without(DayOfWeek) to build patterns incrementally, and the bitwise operators to combine or intersect them.
Build a pattern for Monday, Wednesday, and Friday
var pattern = WeekPattern.Empty
.With(DayOfWeek.Monday)
.With(DayOfWeek.Wednesday)
.With(DayOfWeek.Friday);
Check membership
bool hasFriday = pattern.Contains(DayOfWeek.Friday); // true
Count selected days
int count = pattern.Count; // 3
Enumerate selected days
foreach (DayOfWeek day in pattern)
Console.WriteLine(day);
Constructors
WeekPattern(params DayOfWeek[])
Initializes a new instance of the WeekPattern struct with the specified selected days.
public WeekPattern(params DayOfWeek[] daysOfWeek)
Parameters
Examples
var pattern = new WeekPattern(DayOfWeek.Monday, DayOfWeek.Wednesday, DayOfWeek.Friday);
Console.WriteLine(pattern.Count); // 3
Exceptions
- ArgumentOutOfRangeException
Thrown if any value in
daysOfWeekis outside the valid DayOfWeek range.
WeekPattern(string)
Initializes a new instance of the WeekPattern struct by parsing the specified string.
public WeekPattern(string input)
Parameters
Remarks
This constructor behaves identically to Parse(string), automatically inferring the format from the input string. Use ParseExact(string, string) when the format must be specified explicitly.
Exceptions
- ArgumentNullException
Thrown if
inputis null.- FormatException
Thrown if
inputis not exactly seven characters long or contains invalid characters.
Fields
AllDays
Represents a working-week WeekPattern with every day of the week selected.
public static readonly WeekPattern AllDays
Field Value
Remarks
Corresponds to AllDays.
Empty
Represents a WeekPattern with no days selected.
public static readonly WeekPattern Empty
Field Value
MondayToFriday
Represents a working-week WeekPattern with Monday through Friday selected.
public static readonly WeekPattern MondayToFriday
Field Value
Remarks
This is an alias for Weekdays and corresponds to MondayToFriday.
MondayToSaturday
Represents a working-week WeekPattern with Monday through Saturday selected.
public static readonly WeekPattern MondayToSaturday
Field Value
Remarks
Corresponds to MondayToSaturday.
MondayToThursdayAndSaturday
Represents a working-week WeekPattern with Monday through Thursday and Saturday selected.
public static readonly WeekPattern MondayToThursdayAndSaturday
Field Value
Remarks
Corresponds to MondayToThursdayAndSaturday.
SaturdayToThursday
Represents a working-week WeekPattern with Saturday through Thursday selected.
public static readonly WeekPattern SaturdayToThursday
Field Value
Remarks
Corresponds to SaturdayToThursday.
SaturdayToWednesday
Represents a working-week WeekPattern with Saturday through Wednesday selected.
public static readonly WeekPattern SaturdayToWednesday
Field Value
Remarks
Corresponds to SaturdayToWednesday.
SundayToFriday
Represents a working-week WeekPattern with Sunday through Friday selected.
public static readonly WeekPattern SundayToFriday
Field Value
Remarks
Corresponds to SundayToFriday.
SundayToThursday
Represents a working-week WeekPattern with Sunday through Thursday selected.
public static readonly WeekPattern SundayToThursday
Field Value
Remarks
Corresponds to SundayToThursday.
Weekdays
Represents a WeekPattern with Monday through Friday selected.
public static readonly WeekPattern Weekdays
Field Value
Weekend
Represents a WeekPattern with Saturday and Sunday selected.
public static readonly WeekPattern Weekend
Field Value
Properties
Count
Gets the number of days currently selected.
public int Count { get; }
Property Value
this[DayOfWeek]
Gets whether the specified DayOfWeek is selected.
public bool this[DayOfWeek day] { get; }
Parameters
dayDayOfWeekThe day to query.
Property Value
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayis outside the valid DayOfWeek range.
Methods
CompareTo(WeekPattern)
Compares this instance to a specified WeekPattern and returns an indication of their relative values.
public int CompareTo(WeekPattern other)
Parameters
otherWeekPatternA WeekPattern to compare with this instance.
Returns
- int
A signed integer whose sign indicates whether this instance is less than, equal to, or greater than
other.
CompareTo(byte)
Compares this instance to a specified byte bitmask value and returns an indication of their relative values.
public int CompareTo(byte other)
Parameters
Returns
- int
A signed integer whose sign indicates whether this instance is less than, equal to, or greater than
other.
CompareTo(object?)
Compares this instance to a specified object and returns an indication of their relative values.
public int CompareTo(object? obj)
Parameters
objobjectAn object to compare. Must be a WeekPattern or a byte, or null.
Returns
- int
A signed integer indicating the relative order: greater than zero if this instance is greater than
objorobjis null; zero if equal; less than zero if smaller.
Remarks
Ordering is based on the numeric value of the underlying bitmask and has no inherent day-of-week meaning. It is provided to support sorting and binary-search scenarios where a consistent total order is required. For domain-specific ordering, enumerate the selected days directly.
Exceptions
- ArgumentException
Thrown if
objis not a WeekPattern or byte.
Contains(DayOfWeek)
Determines whether the specified DayOfWeek is selected in this pattern.
public bool Contains(DayOfWeek day)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayis outside the valid DayOfWeek range.
Equals(WeekPattern)
Determines whether the specified WeekPattern is equal to the current instance.
public bool Equals(WeekPattern other)
Parameters
otherWeekPatternThe WeekPattern to compare with this instance.
Returns
Equals(byte)
Determines whether the specified byte bitmask is equal to the underlying bitmask of the current instance.
public bool Equals(byte other)
Parameters
Returns
Equals(object?)
Determines whether the specified object is equal to the current WeekPattern.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare. Only another boxed WeekPattern can be equal; all other types, including a boxed byte and null, return false.
Returns
- bool
true if
objis a WeekPattern with the same selected-day bitmask; otherwise, false.
Remarks
A boxed byte is intentionally not considered equal here so that object-level equality stays symmetric: Equals(object) can never recognize a boxed WeekPattern, so treating the reverse comparison as equal would violate the reflexive-symmetric contract of Equals(object) and corrupt hash-based collections. Use the strongly typed Equals(byte) overload to compare against a raw bitmask.
FromByte(byte)
Creates a WeekPattern from the specified bitmask byte value.
public static WeekPattern FromByte(byte value)
Parameters
valuebyteA byte in the range [0, 127] where each bit represents a day of the week in Sunday-first order.
Returns
- WeekPattern
A WeekPattern corresponding to the supplied bitmask.
Exceptions
- ArgumentOutOfRangeException
Thrown if
valueexceeds 127.
GetEnumerator()
Returns a struct enumerator that yields each selected DayOfWeek in Sunday-first order without allocating.
public WeekPattern.Enumerator GetEnumerator()
Returns
- WeekPattern.Enumerator
A WeekPattern.Enumerator that iterates the selected days. Returning the struct directly avoids the heap allocation that a yield-state-machine enumerator would incur, and the C#
foreachpattern binds to it via pattern matching without going through IEnumerable<T>.
GetHashCode()
Returns a hash code for the current instance derived from the underlying bitmask.
public override int GetHashCode()
Returns
- int
An int hash code consistent with Equals(WeekPattern).
Parse(string)
Converts the string representation of selected days into a WeekPattern, automatically inferring the format.
public static WeekPattern Parse(string input)
Parameters
Returns
- WeekPattern
A WeekPattern corresponding to the selected days in
input.
Remarks
The format is inferred from the length and character patterns of the input. For symbol strings the day ordering is resolved from the first position whose letter is valid in only one layout.
One symbol form is genuinely ambiguous: a string whose only day letter is an 'S' in the final position ("______S")
is a valid Saturday-only pattern in Sunday-first order and a valid Sunday-only pattern in Monday-first order.
Auto-detection resolves it as the Sunday-first reading (Saturday-only), matching the ordering produced by
the default ToString(). Strings produced with the explicit Monday-first format should
be parsed with ParseExact(string, string) and the "M" format, which is the unambiguous
round-trip partner of ToString("M").
Exceptions
- ArgumentNullException
Thrown if
inputis null.- FormatException
Thrown if
inputis incorrectly formatted or cannot be parsed.
ParseExact(string, string)
Converts the string representation of selected days into a WeekPattern using a specified format.
public static WeekPattern ParseExact(string input, string format)
Parameters
inputstringThe input string that represents selected days. Must not be null.
formatstringA format string that defines the day ordering and the character used for unselected days. See ToString(string?, IFormatProvider?) for supported values. Must not be null.
Returns
- WeekPattern
A WeekPattern parsed according to the specified format.
Exceptions
- ArgumentNullException
Thrown if
inputorformatis null.- FormatException
Thrown if
inputorformatis invalid or unrecognized.
ToByte()
Returns a byte representation of the selected days.
public byte ToByte()
Returns
ToInt32()
Returns an int representation of the selected days, for contexts where an integer type is required.
public int ToInt32()
Returns
ToString()
Returns a string representation of the current WeekPattern using the default Sunday-first format with underscore for unselected days.
public override string ToString()
Returns
- string
A seven-character string in Sunday-to-Saturday order where each selected day is represented by its initial letter and each unselected day by
'_'.
ToString(IFormatProvider)
Returns a string representation of the current WeekPattern using the default format and the specified culture-specific formatting information.
public string ToString(IFormatProvider provider)
Parameters
providerIFormatProviderAn IFormatProvider supplying culture-specific formatting information (currently ignored).
Returns
- string
A string representing the selected days in Sunday-to-Saturday order with
'_'for unselected days.
ToString(string)
Returns a string representation of the current WeekPattern using the specified format.
public string ToString(string format)
Parameters
formatstringA format string that defines the day ordering and the character used for unselected days. See ToString(string?, IFormatProvider?) for supported values.
Returns
- string
A formatted string representing the selected days.
Exceptions
- ArgumentException
Thrown if
formatis not recognized.
ToString(string?, IFormatProvider?)
Returns a string representation of the current WeekPattern using the specified format and culture-specific formatting information.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringA format string determining day ordering and the unselected-day placeholder. Supported values:
'S'or's'- Sunday-to-Saturday; unselected ='_'.'M'or'm'- Monday-to-Sunday; unselected ='_'.-
'E','U','D','A'- Sunday-to-Saturday with space, underscore, dash, or asterisk for unselected days respectively. -
'0','1','B', or"01"- Binary:'1'selected,'0'unselected. -
Two-character format: first character
'S'or'M'for day ordering; second character'E','U','D', or'A'for the unselected placeholder.
formatProviderIFormatProviderAn IFormatProvider (currently ignored).
Returns
- string
A formatted seven-character string representing the selected days.
Exceptions
- ArgumentException
Thrown if
formatis not recognized.
TryParse(string, out WeekPattern)
Attempts to parse the string representation of selected days into a WeekPattern, automatically inferring the format.
public static bool TryParse(string input, out WeekPattern result)
Parameters
inputstringThe input string that represents selected days.
resultWeekPatternWhen this method returns, contains the parsed WeekPattern if parsing succeeded; otherwise, contains Empty.
Returns
TryParseExact(string, string, out WeekPattern)
Attempts to parse the string representation of selected days into a WeekPattern using a specified format.
public static bool TryParseExact(string input, string format, out WeekPattern result)
Parameters
inputstringThe input string that represents selected days.
formatstringA format string that defines the day ordering and the character used for unselected days. See ToString(string?, IFormatProvider?) for supported values.
resultWeekPatternWhen this method returns, contains the parsed WeekPattern if parsing succeeded; otherwise, contains Empty.
Returns
With(DayOfWeek)
Returns a new WeekPattern that includes the specified day in addition to those already selected.
If day is already selected the returned instance is equal to the current one.
public WeekPattern With(DayOfWeek day)
Parameters
Returns
- WeekPattern
A new WeekPattern with
dayselected alongside all previously selected days.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayis outside the valid DayOfWeek range.
Without(DayOfWeek)
Returns a new WeekPattern that excludes the specified day. If day is not
selected the returned instance is equal to the current one.
public WeekPattern Without(DayOfWeek day)
Parameters
Returns
- WeekPattern
A new WeekPattern with
dayunselected and all other previously selected days retained.
Exceptions
- ArgumentOutOfRangeException
Thrown if
dayis outside the valid DayOfWeek range.
Operators
operator &(WeekPattern, WeekPattern)
Returns a new WeekPattern containing only the days selected in both operands (set intersection).
public static WeekPattern operator &(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
- WeekPattern
A WeekPattern whose selected days are the intersection of the two operands.
operator |(WeekPattern, WeekPattern)
Returns a new WeekPattern containing all days selected in either operand (set union).
public static WeekPattern operator |(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
- WeekPattern
A WeekPattern whose selected days are the union of the two operands.
operator ==(WeekPattern, WeekPattern)
Determines whether two WeekPattern instances have the same selected days.
public static bool operator ==(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
operator ^(WeekPattern, WeekPattern)
Returns a new WeekPattern containing only the days selected in exactly one of the two operands (symmetric difference).
public static WeekPattern operator ^(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
- WeekPattern
A WeekPattern whose selected days are those that differ between the two operands.
operator >(WeekPattern, WeekPattern)
Determines whether one WeekPattern follows another in the underlying bitmask ordering.
public static bool operator >(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
Remarks
Ordering is based on the numeric value of the underlying bitmask and has no inherent day-of-week meaning.
operator >=(WeekPattern, WeekPattern)
Determines whether one WeekPattern follows or equals another in the underlying bitmask ordering.
public static bool operator >=(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
Remarks
Ordering is based on the numeric value of the underlying bitmask and has no inherent day-of-week meaning.
implicit operator byte(WeekPattern)
Implicitly converts a WeekPattern to its underlying byte bitmask.
public static implicit operator byte(WeekPattern pattern)
Parameters
patternWeekPatternThe WeekPattern to convert.
Returns
operator !=(WeekPattern, WeekPattern)
Determines whether two WeekPattern instances have different selected days.
public static bool operator !=(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
operator <(WeekPattern, WeekPattern)
Determines whether one WeekPattern precedes another in the underlying bitmask ordering.
public static bool operator <(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
Remarks
Ordering is based on the numeric value of the underlying bitmask and has no inherent day-of-week meaning.
operator <=(WeekPattern, WeekPattern)
Determines whether one WeekPattern precedes or equals another in the underlying bitmask ordering.
public static bool operator <=(WeekPattern left, WeekPattern right)
Parameters
leftWeekPatternThe first operand.
rightWeekPatternThe second operand.
Returns
Remarks
Ordering is based on the numeric value of the underlying bitmask and has no inherent day-of-week meaning.
operator ~(WeekPattern)
Returns a new WeekPattern with all seven days toggled - selected days become unselected and vice versa.
public static WeekPattern operator ~(WeekPattern pattern)
Parameters
patternWeekPatternThe WeekPattern to complement.
Returns
- WeekPattern
The bitwise complement of
pattern, masked to the valid seven-day range.
Explicit Interface Implementations
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.
ISerializable.GetObjectData(SerializationInfo, StreamingContext)
Populates a SerializationInfo with the data required to serialize the current WeekPattern.
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoThe SerializationInfo to populate. Must not be null.
contextStreamingContextThe streaming context (not used).
Exceptions
- ArgumentNullException
Thrown if
infois null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |