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

    Console.ReadLine(); 
}
 
Explanation

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.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести