Table of Contents

WeekPattern Struct

Definition

Namespace
Bodu
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
WeekPattern.Enumerator.cs

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

daysOfWeek DayOfWeek[]

An array of DayOfWeek values to mark as selected.

Examples

var pattern = new WeekPattern(DayOfWeek.Monday, DayOfWeek.Wednesday, DayOfWeek.Friday);
Console.WriteLine(pattern.Count); // 3

Exceptions

ArgumentOutOfRangeException

Thrown if any value in daysOfWeek is 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

input string

The input string representing selected days. Must not be null.

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 input is null.

FormatException

Thrown if input is 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

WeekPattern

Remarks

Corresponds to AllDays.

Empty

Represents a WeekPattern with no days selected.

public static readonly WeekPattern Empty

Field Value

WeekPattern

MondayToFriday

Represents a working-week WeekPattern with Monday through Friday selected.

public static readonly WeekPattern MondayToFriday

Field Value

WeekPattern

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

WeekPattern

Remarks

Corresponds to MondayToSaturday.

MondayToThursdayAndSaturday

Represents a working-week WeekPattern with Monday through Thursday and Saturday selected.

public static readonly WeekPattern MondayToThursdayAndSaturday

Field Value

WeekPattern

Remarks

SaturdayToThursday

Represents a working-week WeekPattern with Saturday through Thursday selected.

public static readonly WeekPattern SaturdayToThursday

Field Value

WeekPattern

Remarks

Corresponds to SaturdayToThursday.

SaturdayToWednesday

Represents a working-week WeekPattern with Saturday through Wednesday selected.

public static readonly WeekPattern SaturdayToWednesday

Field Value

WeekPattern

Remarks

Corresponds to SaturdayToWednesday.

SundayToFriday

Represents a working-week WeekPattern with Sunday through Friday selected.

public static readonly WeekPattern SundayToFriday

Field Value

WeekPattern

Remarks

Corresponds to SundayToFriday.

SundayToThursday

Represents a working-week WeekPattern with Sunday through Thursday selected.

public static readonly WeekPattern SundayToThursday

Field Value

WeekPattern

Remarks

Corresponds to SundayToThursday.

Weekdays

Represents a WeekPattern with Monday through Friday selected.

public static readonly WeekPattern Weekdays

Field Value

WeekPattern

Weekend

Represents a WeekPattern with Saturday and Sunday selected.

public static readonly WeekPattern Weekend

Field Value

WeekPattern

Properties

Count

Gets the number of days currently selected.

public int Count { get; }

Property Value

int

An int in the range [0, 7] representing the total number of selected days.

this[DayOfWeek]

Gets whether the specified DayOfWeek is selected.

public bool this[DayOfWeek day] { get; }

Parameters

day DayOfWeek

The day to query.

Property Value

bool

true if day is selected; otherwise, false.

Exceptions

ArgumentOutOfRangeException

Thrown if day is 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

other WeekPattern

A 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

other byte

A byte bitmask to compare with the underlying value of this instance.

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

obj object

An 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 obj or obj is 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 obj is not a WeekPattern or byte.

Contains(DayOfWeek)

Determines whether the specified DayOfWeek is selected in this pattern.

public bool Contains(DayOfWeek day)

Parameters

day DayOfWeek

The day to test. Must be a valid DayOfWeek value.

Returns

bool

true if day is selected; otherwise, false.

Exceptions

ArgumentOutOfRangeException

Thrown if day is outside the valid DayOfWeek range.

Equals(WeekPattern)

Determines whether the specified WeekPattern is equal to the current instance.

public bool Equals(WeekPattern other)

Parameters

other WeekPattern

The WeekPattern to compare with this instance.

Returns

bool

true if both instances have the same selected days; otherwise, false.

Equals(byte)

Determines whether the specified byte bitmask is equal to the underlying bitmask of the current instance.

public bool Equals(byte other)

Parameters

other byte

The byte value to compare.

Returns

bool

true if the bit patterns match; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current WeekPattern.

public override bool Equals(object? obj)

Parameters

obj object

The 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 obj is 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

value byte

A 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 value exceeds 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# foreach pattern 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

input string

The input string that represents selected days. Must not be null.

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 input is null.

FormatException

Thrown if input is 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

input string

The input string that represents selected days. Must not be null.

format string

A 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 input or format is null.

FormatException

Thrown if input or format is invalid or unrecognized.

ToByte()

Returns a byte representation of the selected days.

public byte ToByte()

Returns

byte

A byte whose bits correspond to the selected days in Sunday-first order.

ToInt32()

Returns an int representation of the selected days, for contexts where an integer type is required.

public int ToInt32()

Returns

int

An int whose value equals the underlying bitmask.

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

provider IFormatProvider

An 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

format string

A 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 format is 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

format string

A 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.
formatProvider IFormatProvider

An IFormatProvider (currently ignored).

Returns

string

A formatted seven-character string representing the selected days.

Exceptions

ArgumentException

Thrown if format is 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

input string

The input string that represents selected days.

result WeekPattern

When this method returns, contains the parsed WeekPattern if parsing succeeded; otherwise, contains Empty.

Returns

bool

true if parsing succeeded; otherwise, false.

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

input string

The input string that represents selected days.

format string

A format string that defines the day ordering and the character used for unselected days. See ToString(string?, IFormatProvider?) for supported values.

result WeekPattern

When this method returns, contains the parsed WeekPattern if parsing succeeded; otherwise, contains Empty.

Returns

bool

true if parsing succeeded; otherwise, false.

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

day DayOfWeek

The DayOfWeek to add.

Returns

WeekPattern

A new WeekPattern with day selected alongside all previously selected days.

Exceptions

ArgumentOutOfRangeException

Thrown if day is 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

day DayOfWeek

The DayOfWeek to remove.

Returns

WeekPattern

A new WeekPattern with day unselected and all other previously selected days retained.

Exceptions

ArgumentOutOfRangeException

Thrown if day is 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

left WeekPattern

The first operand.

right WeekPattern

The 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

left WeekPattern

The first operand.

right WeekPattern

The 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

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if both instances are equal; otherwise, false.

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

left WeekPattern

The first operand.

right WeekPattern

The 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

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if left is greater than right; otherwise, false.

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

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if left is greater than or equal to right; otherwise, false.

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

pattern WeekPattern

The WeekPattern to convert.

Returns

byte

A byte representing the selected days.

operator !=(WeekPattern, WeekPattern)

Determines whether two WeekPattern instances have different selected days.

public static bool operator !=(WeekPattern left, WeekPattern right)

Parameters

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if the instances differ; otherwise, false.

operator <(WeekPattern, WeekPattern)

Determines whether one WeekPattern precedes another in the underlying bitmask ordering.

public static bool operator <(WeekPattern left, WeekPattern right)

Parameters

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if left is less than right; otherwise, false.

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

left WeekPattern

The first operand.

right WeekPattern

The second operand.

Returns

bool

true if left is less than or equal to right; otherwise, false.

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

pattern WeekPattern

The 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

info SerializationInfo

The SerializationInfo to populate. Must not be null.

context StreamingContext

The streaming context (not used).

Exceptions

ArgumentNullException

Thrown if info is null.

Applies to

ProductVersions
.NET8, 10