Magic of Inc and Dec Operators in C#

This is my very first blog on C-Sharp Corner. Wanted to start with a little magic/surprising behavior of Increment Operators in C#. Refer below the code snippet, you'll find it interesting:
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       int x=1;  
  6.       int y = 0;  
  7.       y = x++;  
  8.       Console.WriteLine("X:=>{0},Y:=>{1}", x, y);  
  9.       Console.WriteLine();  
  10.       y = ++x;  
  11.       Console.WriteLine("X:=>{0},Y:=>{1}", x, y);  
  12.       Console.WriteLine();  
  13.       Console.WriteLine("X:=>{0},Y:=>{1}", x++, ++y);  
  14.       Console.WriteLine();  
  15.       Console.WriteLine("X:=>{0},Y:=>{1}", ++x, y++);  
  16.       Console.WriteLine();  
  17.       Console.WriteLine("X:=>{0},Y:=>{1}", x, y);  
  18.       Console.WriteLine();  
  19.       Console.Read();  
  20.    }  

It's good to check your knowledge by predicting the results before referring the answers. Below section contains the answers ready for you:
 
The output of the above snippet would be:
X:=>2,Y:=>1
X:=>3,Y:=>3
X:=>3,Y:=>4
X:=>5,Y:=>4
X:=>5,Y:=>5
Ebook Download
View all
Learn
View all