What will be printed out as a result of the following code execution:
public void TestMethod() { 
    int k = 1; 
    Console.WriteLine(k++ + ++k); 


k++ post increment will return 1 and will increase the value of k by 1. ++k pre increment will increase the value of k=2 by 1. So the sum will be equal to 4.

Unary operators are of a higher priority than binary ones. Operators with the same precedence are calculated from right to left. Post increment and pre increment are calculated differently for the same value of the operand.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes