Is it safe to assume DayOfWeek's numeric value?
Solution 1
Yes, this safe, you should not worry about the values changing. Here's how the enum is defined:
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
If those values change in the BCL, a hell lot of a code will break.
Solution 2
Yes always safe,you can go back and front :
return DateTime.Today.AddDays(-1);
and
return DateTime.Today.AddDays(1);
Related videos on Youtube
Yehuda Shapira
Programmer, aspiring writer, hobbyist game-designer. Tends to be tired. Avid coffee-drinker.
Updated on September 14, 2022Comments
-
Yehuda Shapira over 1 year
I have the day of the week stored in a database, where Sunday = 1, Monday = 2 etc.
In a query from the database, I need to convert the day to
System.DayOfWeek
.According to MSDN:
The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
This means that after I query the data, I need to subtract 1 before I convert the result to
DayOfWeek
.Is this safe?
Or do I need to worry about the numeric values ofDayOfWeek
changing?-
L.B over 11 yearsYes, When it is documented as
its value ranges from zero to six
-
-
Ultroman the Tacoman about 8 yearsI'm sorry, but am I the only one who's completely perplexed, that these have been written in alphabetical order, instead of numerical and/or starting from the beginning of the week (sunday or monday)?
-
Kevin B Burns almost 7 yearsI almost down voted you Darin, you made me think and it hurt my brain. Next time, why don't you list them in a proper order you monster! :)