Table of Contents

MonthSet Struct

Definition

Namespace
Bodu
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.3.0
Source
MonthSet.Enumerator.cs

Represents an immutable set of months of the year, 1 (January) to 12 (December).

public readonly struct MonthSet : ICalendarValueSet<MonthSet, int>, IEquatable<MonthSet>, IEqualityOperators<MonthSet, MonthSet, bool>, IBitwiseOperators<MonthSet, MonthSet, MonthSet>, ISpanParsable<MonthSet>, IParsable<MonthSet>, IUtf8SpanParsable<MonthSet>, ISpanFormattable, IFormattable, IUtf8SpanFormattable, IEnumerable<int>, IEnumerable
Implements
Inherited Members
Extension Methods

Examples

var selected = new MonthSet(1, 4, 7, 10);
MonthSet parsed = MonthSet.Parse("1,4,7,10");

bool due = selected.Contains(DateTime.Today.Month);
bool same = selected == parsed; // true

Remarks

A MonthSet holds one bit per month, so it never allocates, and testing, adding or removing a month, or combining two sets with |, &, ^ and ~, is a single bitwise operation. Contains(int) answers false for any value outside 1 to 12 rather than throwing, which keeps a membership test against Month to a comparison and a bit test.

The text form lists the months in ascending order, separated by commas, each run of two or more consecutive months written as an inclusive range, so "1,4,7,10" selects the first month of each quarter. 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 month without ranges, a binary form with one character per month, or a letter mask of the months' initials, such as "JFM________D", 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 month 1 + n.

Constructors

MonthSet(params int[]?)

Initializes a new instance of the MonthSet struct that selects the specified months.

public MonthSet(params int[]? months)

Parameters

months int[]

The months to select, or null for none.

Remarks

A month may appear more than once, and the order is immaterial.

Exceptions

ArgumentOutOfRangeException

Thrown when a value in months is less than 1 or greater than 12.

Properties

Count

Gets the number of months the set selects.

public int Count { get; }

Property Value

int

A number from 0 to 12.

Methods

Contains(int)

Determines whether the set selects the specified month.

public bool Contains(int month)

Parameters

month int

The month to test.

Returns

bool

true when the set selects month; false when it does not, including when month is outside 1 to 12.

Equals(MonthSet)

Determines whether this set selects the same months as another.

public bool Equals(MonthSet other)

Parameters

other MonthSet

The set to compare with.

Returns

bool

true when the sets are equal; otherwise false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetEnumerator()

Returns an enumerator that yields the selected months in ascending order without allocating.

public MonthSet.Enumerator GetEnumerator()

Returns

MonthSet.Enumerator

An enumerator over the selected months.

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 MonthSet, detecting the form.

public static MonthSet Parse(ReadOnlySpan<byte> utf8Text)

Parameters

utf8Text ReadOnlySpan<byte>

The UTF-8 text to convert.

Returns

MonthSet

The set the text describes.

Remarks

The text is decoded and read as Parse(string) reads a string.

Exceptions

FormatException

Thrown when utf8Text is not valid UTF-8, or is neither the binary form, the letter mask, nor a list of values and ranges from 1 to 12.

Parse(ReadOnlySpan<char>)

Converts a span of characters in one of the forms ToString(string?, IFormatProvider?) writes into a MonthSet, detecting the form.

public static MonthSet Parse(ReadOnlySpan<char> s)

Parameters

s ReadOnlySpan<char>

The characters to convert.

Returns

MonthSet

The set the characters describe.

Remarks

The characters are read as Parse(string) reads a string.

Exceptions

FormatException

Thrown when s is neither the binary form, the letter mask, nor a list of values and ranges from 1 to 12.

Parse(string)

Converts text in one of the forms ToString(string?, IFormatProvider?) writes into a MonthSet, detecting the form.

public static MonthSet Parse(string s)

Parameters

s string

The text to convert, such as "1,4,7,10".

Returns

MonthSet

The set the text describes.

Remarks

A text of exactly 12 characters, each 0 or 1, is the binary form, month 1 first. A text of 12 characters, each a month's initial in its place or a placeholder, is the letter mask, January first, such as "JFM________D": the initials are read in either case, and the placeholder may be _, -, * or a space, the same one throughout. Any other text is a comma-separated list of months 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 forms are tested in that order, and the binary form and the letter mask read the text as given, so text made only of 0 and 1 is a list only when it is not 12 characters long, and a mask with whitespace around it is not a mask. ParseExact(string, string) with the "G" format reads any text as a list.

Exceptions

ArgumentNullException

Thrown when s is null.

FormatException

Thrown when s is not the binary form, the letter mask, or a list of values and ranges from 1 to 12.

ToString()

Returns the canonical text form of the set: the selected months in ascending order, separated by commas, each run of two or more consecutive months written as an inclusive range.

public override string ToString()

Returns

string

The text form, such as "1-3,12", 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

format string

The format, or null or empty for the default.

Returns

string

The text.

Remarks

The formats are described at ToString(string?, IFormatProvider?).

Exceptions

FormatException

Thrown when format is not a supported format.

ToString(string?, IFormatProvider?)

Returns the set as text in the specified format.

public string ToString(string? format, IFormatProvider? formatProvider)

Parameters

format string

The format, or null or empty for the default.

formatProvider IFormatProvider

Ignored; the text does not depend on culture.

Returns

string

The text.

Remarks

The formats, whose letters are read in either case, are:

FormatText
G, the default The months in ascending order, separated by commas, each run of two or more consecutive months as an inclusive range: "1-3,12".
LEvery selected month listed, without ranges: "1,2,3,12".
B, 0, 1, 01 Binary, one character per month from 1 to 12, 1 first, with 1 for a selected month: "111000000001".
J A twelve-character letter mask, January first, with each selected month's initial and _ for a month not selected: "JFM________D".
E, U, D, A The letter mask with a space, _, - or * respectively for a month not selected.
J followed by E, U, D or AThe letter mask with that placeholder: "JD" writes "JFM--------D".

Exceptions

FormatException

Thrown when format is not a supported format.

ToUInt64()

Returns the set's bits, bit n selecting month 1 + n.

public ulong ToUInt64()

Returns

ulong

The bits, with nothing set above bit 11.

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

utf8Destination Span<byte>

The span to write to.

bytesWritten int

When this method returns true, the number of bytes written; otherwise 0.

format ReadOnlySpan<char>

The format, or an empty span for the default.

provider IFormatProvider

Ignored; the text does not depend on culture.

Returns

bool

true when the text fits in utf8Destination; otherwise false, and utf8Destination is 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 format is 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

destination Span<char>

The span to write to.

charsWritten int

When this method returns true, the number of characters written; otherwise 0.

format ReadOnlySpan<char>

The format, or an empty span for the default.

provider IFormatProvider

Ignored; the text does not depend on culture.

Returns

bool

true when the text fits in destination; otherwise false, and destination is left unchanged.

Remarks

The text is the one ToString(string?, IFormatProvider?) returns for the same format.

Exceptions

FormatException

Thrown when format is not a supported format.

TryParse(ReadOnlySpan<byte>, out MonthSet)

Attempts to convert UTF-8 text in one of the forms ToString(string?, IFormatProvider?) writes into a MonthSet, detecting the form.

public static bool TryParse(ReadOnlySpan<byte> utf8Text, out MonthSet result)

Parameters

utf8Text ReadOnlySpan<byte>

The UTF-8 text to convert.

result MonthSet

When this method returns true, the set the text describes; otherwise Empty.

Returns

bool

true when utf8Text is valid UTF-8 for the binary form, the letter mask, or a list of values and ranges from 1 to 12; otherwise false.

Remarks

The text is decoded and read as Parse(string) reads a string.

TryParse(ReadOnlySpan<char>, out MonthSet)

Attempts to convert a span of characters in one of the forms ToString(string?, IFormatProvider?) writes into a MonthSet, detecting the form.

public static bool TryParse(ReadOnlySpan<char> s, out MonthSet result)

Parameters

s ReadOnlySpan<char>

The characters to convert.

result MonthSet

When this method returns true, the set the characters describe; otherwise Empty.

Returns

bool

true when s is the binary form, the letter mask, or a list of values and ranges from 1 to 12; otherwise false.

Remarks

The characters are read as Parse(string) reads a string.

TryParse(string?, out MonthSet)

Attempts to convert text in one of the forms ToString(string?, IFormatProvider?) writes into a MonthSet, detecting the form.

public static bool TryParse(string? s, out MonthSet result)

Parameters

s string

The text to convert, such as "1,4,7,10".

result MonthSet

When this method returns true, the set the text describes; otherwise Empty.

Returns

bool

true when s is the binary form, the letter mask, or a list of values and ranges from 1 to 12; otherwise false.

Remarks

The text is read as Parse(string) reads it.

With(int)

Returns a set that also selects the specified month.

public MonthSet With(int month)

Parameters

month int

The month to add.

Returns

MonthSet

A set that selects month and every month this set selects.

Exceptions

ArgumentOutOfRangeException

Thrown when month is less than 1 or greater than 12.

Without(int)

Returns a set that no longer selects the specified month.

public MonthSet Without(int month)

Parameters

month int

The month to remove.

Returns

MonthSet

A set that selects every month this set selects except month.

Exceptions

ArgumentOutOfRangeException

Thrown when month is less than 1 or greater than 12.

Explicit Interface Implementations

All

Gets the set that selects every month from 1 to 12.

static MonthSet All { get; }

Returns

MonthSet

Empty

Gets the set that selects no month.

static MonthSet Empty { get; }

Returns

MonthSet

FromUInt64(ulong)

Creates a set from its bits, bit n selecting month 1 + n.

static MonthSet FromUInt64(ulong bits)

Parameters

bits ulong

The bits.

Returns

MonthSet

The set that bits describe.

Exceptions

ArgumentOutOfRangeException

Thrown when bits sets a bit above bit 11, which selects no month.

ParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>)

Converts a span of characters in the specified format into a MonthSet.

static MonthSet ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format)

Parameters

s ReadOnlySpan<char>

The characters to convert.

format ReadOnlySpan<char>

The format of s, one of those ToString(string?) writes.

Returns

MonthSet

The set the characters describe.

Remarks

The characters are read as ParseExact(string, string) reads a string.

Exceptions

FormatException

Thrown when format is not a supported format, or s is not text in that format.

ParseExact(string, string)

Converts text in the specified format into a MonthSet.

static MonthSet ParseExact(string s, string format)

Parameters

s string

The text to convert.

format string

The format of s, one of those ToString(string?) writes.

Returns

MonthSet

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, 12 characters with no whitespace. "J" reads the letter mask with any one placeholder throughout, and "E", "U", "D" or "A", alone or after J, reads it with that placeholder alone. The format letters and the mask's initials are read in either case.

Exceptions

ArgumentNullException

Thrown when s or format is null.

FormatException

Thrown when format is not a supported format, or s is 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<MonthSet>.Parse(string, IFormatProvider)

Parses a string into a value.

static MonthSet IParsable<MonthSet>.Parse(string s, IFormatProvider provider)

Parameters

s string

The string to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

MonthSet

The result of parsing s.

Exceptions

ArgumentNullException

s is null.

FormatException

s is not in the correct format.

OverflowException

s is not representable by MonthSet.

IParsable<MonthSet>.TryParse(string, IFormatProvider, out MonthSet)

Tries to parse a string into a value.

static bool IParsable<MonthSet>.TryParse(string s, IFormatProvider provider, out MonthSet result)

Parameters

s string

The string to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

result MonthSet

When this method returns, contains the result of successfully parsing s or an undefined value on failure.

Returns

bool

true if s was successfully parsed; otherwise, false.

ISpanParsable<MonthSet>.Parse(ReadOnlySpan<char>, IFormatProvider)

Parses a span of characters into a value.

static MonthSet ISpanParsable<MonthSet>.Parse(ReadOnlySpan<char> s, IFormatProvider provider)

Parameters

s ReadOnlySpan<char>

The span of characters to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

MonthSet

The result of parsing s.

Exceptions

FormatException

s is not in the correct format.

OverflowException

s is not representable by MonthSet.

ISpanParsable<MonthSet>.TryParse(ReadOnlySpan<char>, IFormatProvider, out MonthSet)

Tries to parse a span of characters into a value.

static bool ISpanParsable<MonthSet>.TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out MonthSet result)

Parameters

s ReadOnlySpan<char>

The span of characters to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

result MonthSet

When this method returns, contains the result of successfully parsing s, or an undefined value on failure.

Returns

bool

true if s was successfully parsed; otherwise, false.

IUtf8SpanParsable<MonthSet>.Parse(ReadOnlySpan<byte>, IFormatProvider)

Parses a span of UTF-8 characters into a value.

static MonthSet IUtf8SpanParsable<MonthSet>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider provider)

Parameters

utf8Text ReadOnlySpan<byte>

The span of UTF-8 characters to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about utf8Text.

Returns

MonthSet

The result of parsing utf8Text.

Exceptions

FormatException

utf8Text is not in the correct format.

OverflowException

utf8Text is not representable by MonthSet.

IUtf8SpanParsable<MonthSet>.TryParse(ReadOnlySpan<byte>, IFormatProvider, out MonthSet)

Tries to parse a span of UTF-8 characters into a value.

static bool IUtf8SpanParsable<MonthSet>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider provider, out MonthSet result)

Parameters

utf8Text ReadOnlySpan<byte>

The span of UTF-8 characters to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about utf8Text.

result MonthSet

On return, contains the result of successfully parsing utf8Text or an undefined value on failure.

Returns

bool

true if utf8Text was successfully parsed; otherwise, false.

TryParseExact(ReadOnlySpan<char>, ReadOnlySpan<char>, out MonthSet)

Attempts to convert a span of characters in the specified format into a MonthSet.

static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, out MonthSet result)

Parameters

s ReadOnlySpan<char>

The characters to convert.

format ReadOnlySpan<char>

The format of s, one of those ToString(string?) writes.

result MonthSet

When this method returns true, the set the characters describe; otherwise Empty.

Returns

bool

true when format is a supported format and s is text in it; otherwise false.

Remarks

The characters are read as ParseExact(string, string) reads a string.

TryParseExact(string?, string?, out MonthSet)

Attempts to convert text in the specified format into a MonthSet.

static bool TryParseExact(string? s, string? format, out MonthSet result)

Parameters

s string

The text to convert.

format string

The format of s, one of those ToString(string?) writes.

result MonthSet

When this method returns true, the set the text describes; otherwise Empty.

Returns

bool

true when format is a supported format and s is text in it; otherwise false.

Remarks

The text is read as ParseExact(string, string) reads it.

operator &(MonthSet, MonthSet)

Returns the months that both sets select.

static MonthSet operator &(MonthSet left, MonthSet right)

Parameters

left MonthSet

The first set.

right MonthSet

The second set.

Returns

MonthSet

The intersection of left and right.

operator |(MonthSet, MonthSet)

Returns the months that either set selects.

static MonthSet operator |(MonthSet left, MonthSet right)

Parameters

left MonthSet

The first set.

right MonthSet

The second set.

Returns

MonthSet

The union of left and right.

operator ==(MonthSet, MonthSet)

Determines whether two sets select the same months.

static bool operator ==(MonthSet left, MonthSet right)

Parameters

left MonthSet

The first set.

right MonthSet

The second set.

Returns

bool

true when the sets are equal; otherwise false.

operator ^(MonthSet, MonthSet)

Returns the months that exactly one of the sets selects.

static MonthSet operator ^(MonthSet left, MonthSet right)

Parameters

left MonthSet

The first set.

right MonthSet

The second set.

Returns

MonthSet

The symmetric difference of left and right.

operator !=(MonthSet, MonthSet)

Determines whether two sets select different months.

static bool operator !=(MonthSet left, MonthSet right)

Parameters

left MonthSet

The first set.

right MonthSet

The second set.

Returns

bool

true when the sets differ; otherwise false.

operator ~(MonthSet)

Returns the months from 1 to 12 that the set does not select.

static MonthSet operator ~(MonthSet value)

Parameters

value MonthSet

The set.

Returns

MonthSet

The complement of value within 1 to 12.

Applies to

ProductVersions
.NET8, 10