Ahmed El-Halwagy’s Blog

Posts Tagged ‘C#-mysteries

A little ago Eric Lippert took a stand to the programming myths that people have about c#. Starting by his awesome The stack is an implementation detail
then Not everything derives from object and today’s Precedence vs Order, Redux. I have to say it was an awesome job, very nice articles -just like everything Eric writes. I strongly recommend that you scan through these articles, a lot of mysteries to be unveiled there. I’m not going to repeat what Eric said because I’m too proud to do! Well, not really! It’s just I’m not as good as Eric to think of providing new versions of his articles (what new would these new versions have anyway?) I just want to stop by the last article Precedence vs Order, Redux. I had a nice 12 parts conversation on facebook with one of my friends. At first I posted a Twitter post with a question and a link to Eric’s article. I got a comment from one of my friends –who stands to be one of the very best students at Century 21, IBM. The man’s comment was “value = 1”. Nice and easy! Well, that wasn’t what I expected from this friend -I know the answer, I run the sample code, and seen the value of value :), and I read Eric’s article. What I expected him to say is 0. Because as far as I know, this program should work this way:

1- Get the value of arr[0] .. the inner expression arr[0] .. 0 returned

2- Get the value of arr[0] again .. the outer arr[arr[0]] .. still 0 returned

3- Now assign the value of 0 to the variable value.

4- Now and not before now, Increment arr[0].

Apparently what I know is wrong. The increment didn’t happen the moment I expected it to happen –after assigning the value. Actually it happened before assigning the value. Frankly I don’t understand why!? I tried to search the C# spec and couldn’t find anything to state strictly when the increment should happen in case of post increment and pre-increment.
Another interesting and confusing case is a snippet I saw in a comment by Peter Ibboston on the same article by Eric:

The one that confused me is this C# code which gives different results in C & C#

int[] data={11,22,33};int i=1;data[i++]=data[i]+5;

I couldn’t find a bit in the C# spec that said anything about when increment should happen. I had presumed that the rvalue would be evaluated first followed by the lvalue.

Any ideas where I missed it.

To finalize I would say, there’s no documentation to specifically tell when the increment will happen –AFAIK of course. Eric emphasized an interesting statement in his article too, he said:

I emphasize that the logical sequence is well-defined because the compiler, jitter and processor are all allowed to change the actual order of events for optimization purposes, subject to the restriction that the optimized result must be indistinguishable from the required result in single-threaded scenarios.

The compiler or the Jitter or the processor are all able to modify the logical sequence for optimization purposes. The optimization shouldn’t affect the output in a single-threaded application, however, in a multi-threaded app, it might be observable. Yeah I see. I still don’t know why is this working this way? When exactly the increment is happening?

What do you think dear reader?

Please help me clarify the facts, and eliminate these confusions.



July 2018
« Nov    



Recent Twitter Updates