What is the output of: int x = 5; Console.WriteLine(x++ + ++x);?
Correct answer D. 12
C# evaluates operands strictly left to right. x++ yields the old value 5 and then sets x to 6. Next, ++x increments x to 7 and yields 7. The sum is 5 + 7 = 12. Unlike C and C++, where modifying a variable twice in one expression is undefined, C# defines this order exactly, so the result is always 12.
Answer checked How we write and check questions
Discussion