Is it safe to assume DayOfWeek's numeric value?

14,693

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);
Share:
14,693

Related videos on Youtube

Yehuda Shapira
Author by

Yehuda Shapira

Programmer, aspiring writer, hobbyist game-designer. Tends to be tired. Avid coffee-drinker.

Updated on September 14, 2022

Comments

  • Yehuda Shapira
    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 of DayOfWeek changing?

    • L.B
      L.B over 11 years
      Yes, When it is documented as its value ranges from zero to six
  • Ultroman the Tacoman
    Ultroman the Tacoman about 8 years
    I'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
    Kevin B Burns almost 7 years
    I 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! :)