I have the enum:
[Flags, Serializable,]
public enum WeekDays {
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
WeekendDays = Sunday | Saturday,
WorkDays = Monday | Tuesday | Wednesday | Thursday | Friday,
EveryDay = WeekendDays | WorkDays
}
And, I have property WeekDays
in a class that contain value of WeekDays
enum:
public int WeekDays {get; set;}
For example WorkDays
contain 62( from Monday to Friday).
How to check that current WeekDays
property contain current day?
Enum has method HasFlag
to determine whether one or more bit fields are set in the current instance.
Use the bitwise operator &
to see if a value is part of a set:
var today = WeekDays.Thursday;
var workdays = WeekDays.WorkDays;
if((today & workdays) == today) {
// today is a workday
}
if((today & WeekDays.Friday) == today) {
// it's friday
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With