Strange DateTime Calculation Problem

I probably worked too much, but can someone explain to me the following, taken from the Immediate window?

(int)DateTime.Now.Date.DayOfWeek = 4

and

(int)DayOfWeek.Sunday = 0

and

(int)DateTime.Now.Date.DayOfWeek - (int)DayOfWeek.Sunday = 4

but
(int)DayOfWeek.Sunday - (int)DateTime.Now.Date.DayOfWeek = Could not evaluate expression `

Thanks for reading.

EDIT:

This is an Immediate window that gives me this strange result, not regular code.

Screenshot: http://ploader.net/files/0c2556df475b3075634d7fd2b0575794.PNG

EDIT2:

The community seems to consider it a bug in VS2010. I wonder if @EricLippert or @JonSkeet can save a minute to confirm this, or, if not, offer an explanation of this behavior?

+7
source share
2 answers

It is similar to constant 0 and non-literal value. The following works fine:

 int zero = 0; zero - (int)DateTime.Now.Date.DayOfWeek -4 

So far the following is being done:

 int four = 4; 0 - four Could not evaluate expression 

Update: I could not find a similar error report, so I created one: https://connect.microsoft.com/VisualStudio/feedback/details/679501/integer-literal-0-integer-variable-could-not-evaluate- expression-immediate-window

Update No. 2: Microsoft can reproduce the problem and resolved it as "Do not fix", which means hope for the next version of Visual Studio, but not for VS2010.

+5
source

I have no idea, this seems like a mistake.

 // This doesn't work 0 - (int)DateTime.Now.Date.DayOfWeek // But loads of similar variations do: 1 - (int)DateTime.Now.Date.DayOfWeek -1 - (int)DateTime.Now.Date.DayOfWeek a - (int)DateTime.Now.Date.DayOfWeek 0 - (int)DayOfWeek.Thursday 

In any case, everything behaves as expected in the compiled code.

+3
source

All Articles