Decimal ternar not working

I am trying to use ternary to assign a decimal type. This does not work for me. I'm going crazy?

Here is a screenshot of my debugging. Before I take a step, you will see all the value. enter image description here

And after I take a step, this is the value. This is not even one of the viable options (for example, 1 or 2000). enter image description here

Is there some weird decimal restriction that I don't know about? When I break it down into a complete logical representation of if / else, it works fine. The only thing I can guess is that I recently installed the .NET Framework 4.5.

UPDATE

I cleared the solution and made sure that I was running code that was compiled in debug mode, as recommended in the comments. None of them seemed to change anything.

I was curious when I noticed that all my unit tests still passed. After a little closer look, I found that when I stepped again (that is, passed through memberItems.Add), the price magically had the right value in it.

Does .Net do some kind of deferred resolution of ternary operators, similar to the yield command in iterator blocks? I have never noticed this before, but I do not know what else could be. I believe that I could still work with code compiled in release mode randomly. I made more gross mistakes after triple checking myself.

+6
source share
2 answers

There is no way to diagnose the code in the screenshot, so just guess.

You cannot always fully rely on what the expression of the clock tells you. The first possible failure mode is optimized debugging code. A local variable, such as price, is typically optimized by the jitter optimizer to be stored in the processor register instead of the stack. The watch expression will show the value of the location of the stack, not the value of the processor register. C 0 is the general result. The only real protection you have against this is only the debugging code that was created by the Debug configuration.

The second failure mode is a way to evaluate the clock. The CLR starts a dedicated thread when it detects an attached debugger. The debugger can then use this thread to evaluate clock expressions. This may go wrong if the variable has thread similarity. Common cases are variables that are [ThreadStatic] or are properties of COM objects.

+6
source

I had the same problem and I also thought that I was crazy.

I found that changing my ASP.NET application to use "Visual Studio Developer Server" instead of IIS fixes it. It's a pity because I like to use IIS as something closer that happens in production.

0
source

Source: https://habr.com/ru/post/923482/


All Articles