0

Giving the following example:

int example1 = 1;
int example2 = 10;

Console.WriteLine(example1);
Console.WriteLine(example2);
Console.WriteLine(example1 = example2++);
Console.WriteLine(example1);
Console.WriteLine(example2);
Console.ReadKey();

Why is the return 1, 10, 10, 11?

I would expect example1 to become equal to the new value of example2, in this case 11.

The ++ seems to take effect after the set of example1.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Philiop
  • 473
  • 3
  • 14
  • 7
    Because it's after the variable name. If you do `++example2` it would be done before. https://msdn.microsoft.com/en-GB/library/36x43w8w.aspx – DavidG Oct 20 '15 at 10:26
  • I googled several varients and couldn't find an answer, cheers for posting the answer. Makes sense when you read it that way. Never come across it in years, just come across it in some legacy code. – Philiop Oct 20 '15 at 10:31
  • Why not just look at the C# documentation, it's pretty obvious on there? – DavidG Oct 20 '15 at 10:31

0 Answers0