8.5. The Increment and Decrement Operators

The ++ and -- operators increment and decrement their operands, respectively, by 1. These are unary operators. They can be used in both prefix and postfix mode. In prefix mode, the result of the operation is the value of the operand after it has been incremented or decremented; in postfix mode, the result of the operation is the value of the operand before it has been incremented or decremented. Listing 8.5 illustrates this point.

Listing 8.5. Using the ++ and -- Operators (C#)
using System;

class Test {
  public static void Main() {
      double x;
      x = 1.5;
      Console.WriteLine(++x);
      Console.WriteLine(--x);
      Console.WriteLine(x++);
      Console.WriteLine(x--);
  Console.WriteLine(x);
    }
}

The output of Listing 8.5

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.