While I am testing post increment operator in a simple console application, I realized that I did not understand full concept. It seems weird to me:
int i = 0;
bool b = i++ == i;
Console.WriteLine(b);
The output has been false. I have expected that it would be true. AFAIK, at line 2, because of the post increment, compiler does comparison and assigned b to true, after i incremented by one. But obviously I am wrong. After that I modify the code like that:
int i = 0;
bool b = i == i++;
Console.WriteLine(b);
This time output has been true. What did change from first sample?
bool b = i == i++;
i.e. 0 == 0 which is true and then increment happens. like first the value is compared and then i is incremented But in first case it is happening other way round likebool b = i++ == i;
i.e. 0 == 1 it first read 0 then increment i and then compare it by that time i is incremented to 1 that is why he is getting false. – Pillory