Ahh so this is interesting. As I wrote this out, I assumed there was something under the hood causing the first item to be skipped, but it was not skipped. In fact, it appears to be a bug in Microsoft's docs???
Let me explain. In
DayOfWeek from
system.dayofweek the index of the enum starts with its day starting on Sunday which its index is 0 :
[System.Runtime.InteropServices.ComVisible(true)]
public enum DayOfWeek {
Sunday = 0,
Whereas
Day from
System.Windows.Forms does not hold the same index and it starts on a Monday giving the illusion it was skipping the first item :
public enum Day {
Monday = 0,
Reference for
Day :
Reference Source
Reference for
DayOfWeek :
Reference Source
As you can see, this is distinctly different. Whereas I blindly assumed Microsoft would have only ever defined Day as an enum once in the framework. And I also assumed that
DayOfWeek would also have been some inherited class or struct for the
Day enum. Initially you'd think; well i did, and so I thought that the enums would have been referred to only once. But having two enums technically referring to the same thing which are days of the week, should be holding the same index and starting on the same day, so that the iteration of both
DaysOfWeek and
Day would be the same. This will only happen if the week starts on the same day which they do not. I'm considering submitting this as a bug, or at best declare it defective or contradictory code. Why? Well unless you look under the hood at the reference docs which I normally do, you would never know why my original code appeared to work. I'm attaching some code samples below to compare the output of all three variations. :
Debug.WriteLine("......... Checking DayOfWeek ..........");
List<DayOfWeek> lDayOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };
foreach (DayOfWeek day in lDayOfWeek)
{
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Friday)
Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
else
Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
}
Debug.WriteLine("......... Checking Day ..........");
List<Day> lDay = new List<Day> { Day.Sunday, Day.Monday, Day.Tuesday,
Day.Wednesday, Day.Thursday, Day.Friday, Day.Saturday };
foreach (Day day in lDay)
{
if (day >= Day.Monday && day <= Day.Friday)
Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
else
Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
}
Debug.WriteLine("......... Checking Day in DaysOfWeek [Causes incorrect results]..........");
List<DayOfWeek> DOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };
foreach (Day day in DOfWeek)
{
if (day >= Day.Monday && day <= Day.Friday)
Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
else
Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
}
It was only when Skydiver pointed out above p/#9 that the order of the enums set up the values, and it was this that got me thinking to check the references to see how it was actually operating. Moral of the story, write it as Skydiver has done on p/#11 or use one of my first or second variation above and don't use
DayOfWeek with Day because they are both declared to start on different days. Sunday and Monday, both starting indexes are zero for both days.
If anyone can explain to me why these emums are setup to use different days and why one of them uses a default value, I'd love to know.
Edit fixed broken code.