Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to assume DayOfWeek's numeric value?

Tags:

c#

enums

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?

like image 881
Yehuda Shapira Avatar asked Dec 02 '12 14:12

Yehuda Shapira


2 Answers

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.

like image 170
Darin Dimitrov Avatar answered Oct 18 '22 17:10

Darin Dimitrov


Yes always safe,you can go back and front :

return DateTime.Today.AddDays(-1);

and

return DateTime.Today.AddDays(1);
like image 45
sajanyamaha Avatar answered Oct 18 '22 19:10

sajanyamaha