1

I'm not sure if I should actually be posting the question on this site, but here it goes. I'm reading Essential C# 6.0 and I encountered this paragraph

The result of the prefix operators is the value that the variable had before it was incremented or decremented. The result of the postfix operators is the value that the variable had after it was incremented or decremented.

I think read this at least 10 times but to me it seams to be exactly the opposite of the code below (which is not from the book). Can anyone explain if it's something that I'm getting wrong or is it just a mistake in the book ? I've also checked the errata btw and I couldn't find this there.

using System;

public class Program
{
    public static void Main()
    {
        var a = 23;
        var b = 23;

        var c = a++; //postfix 
        var d = ++b; //prefix

        Console.WriteLine(c); //23
        Console.WriteLine(d); //24

    }
}
Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
  • @Sinatr the reason I'm asking is because I'm not sure I'm understanding the the question correct and if this question is actually a duplicate it would actually be of [this one](https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i/3346729#3346729) and the second answer would be the correct one – Bobby Tables Sep 29 '17 at 12:36

1 Answers1

4

Of course it's the opposite and it's also mentioned on MSDN:

++variable and variable++

  1. The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.
  2. The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

I haven't read that book, but if it's mentioned there then it's a big mistake.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    [Here](http://ptgmedia.pearsoncmg.com/images/9780134141046/samplepages/9780134141046.pdf) is the book (search for "result of the prefix") by "Mark Michaelis with Eric Lippert".. interesting... – Sinatr Sep 29 '17 at 12:39