Search code examples
c#unary-operator

unary increment operator i++ in C#


In my last interview i found this tricky question. after these two lines what will be the value of i.

int i=c;
//c is a constant
i=i++;

where c is a constant(where c is initialised before). please give me step by step answer rather than one word answer.


Solution

  • finally value of i remains same as c.

    Explanation:- when i is assigned with i++;

    step 1. first i++ return value c(but not assigned to left-hand operand i).

    step 2. then i++ increments value of i to c+1.

    step 3. i is assigned with value c that is returned in step1.

    In effect value of i remains same but some where along the execution it was c+1, But finally it is assigned with value c.