Reputation: 125524
i want to understand a line in c#,
Int32 dayOfWeekIndex = (Int32)DateTime.Now.DayOfWeek + 1;
what this return for example if we run it today ?
I do not have the option to run this code.
Upvotes: 0
Views: 191
Reputation: 8053
0 is sunday, 6 is saturday, so if you run the code today (thursday), you'll get 5
http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek%28v=VS.80%29.aspx
Upvotes: 1
Reputation: 27609
http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx
An enumerated constant that indicates the day of the week of this DateTime value.
The value of the constants in the DayOfWeek 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).
SO today being thursday means that DayOfWeek will be 4. So day of week+1 will be 5.
I did not run this code. I just used google and msdn.
Upvotes: 1
Reputation: 16984
DateTime.DayOfWeek is an enum for the days of the week with values such as DayOfWeek.Monday, DayOfWeek.Tuesday etch. It's underlying type is integers which is the default for enums. Therefore this code will return the underlying integer for whichever day it current is plus one.
Upvotes: 1
Reputation: 39329
DateTime.Now returns the current date. The DayOfWeek property returns the day-of-the-week (monday, tuesday, etc) of that date as an enum value.
The cast to Int32 turns that enum value into an int (where sunday = 0).
Then 1 is added so sunday will end up as 1 and thursday as 5.
Upvotes: 1
Reputation: 23624
According to MSDN: "The value of the constants in the DayOfWeek enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero"
So you see usage of enumeration that is casted to int. For Sunday + 1 = 1 ...
Upvotes: 1
Reputation: 166416
Google is your friend X-)
If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
Upvotes: 5