2007/11/21

Brainstorming: Prefix and Postfix Operators

Another interested code segment talks about prefix and postfix operators:
class IncrementExample
{
 public static void Main()
 {
   int x = 1;

   Console.WriteLine("{0}, {1}", x++, x++);
   Console.WriteLine("{0}, {1}", ++x, ++x);
 }
}
The result is
1, 2
4, 5

Since x++ has a postfix operator, that means x will give its value to Console.WriteLine first, then perform the ++ operation. In contrast, prefix operator will perform ++ operation first, then give the value to Console.WriteLine.

No comments:

Post a Comment