i want to understand a line in c#,
Int32 dayOfWeekIndex = (Int32)DateTime.Now.DayOfWeek + 1;
what this return for example if we run it t开发者_如何学Gooday ?
I do not have the option to run this code.
Google is your friend X-)
See DayOfWeek Enumeration
If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
It depends on where in the world you are and what your computer clock is set up to.
It returns the value of the DayOfWeek
enum corresponding to the time it was run, plus 1. Sunday is 0, Saturday is 6.
So, for Thursday, dayOfWeekIndex
would be 5.
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 ...
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.
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.
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.
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
精彩评论