X=X+1 is equivalent to

A) X++
B) X+=1
C) Both A & B

Showing Answers 1 - 9 of 9 Answers

sabir

  • Jan 13th, 2006
 

both r same and valid

  Was this answer useful?  Yes

The Answer is A X++ as it is the next increment value of X and X+=1 is it shows the initial value of X and then again an incremented value where as X= X+1 is only equal to X++.

  Was this answer useful?  Yes

Sreerenj

  • Mar 24th, 2006
 

Both A and B are correct, but it depends on where you are using it. Same like pre and post increment (e.g. i++, ++i)

For example if you are using it like
int x = 10;
x++; // or x+=1;
Console.Write(x);

both will give the same output, but if you are using like
Console.Write(x++);
then the output will be 10 but for x+=1 then it will be 11 that means it is equalent to post increment ++x;

So the currect answer depends on the context where you are using

  Was this answer useful?  Yes

dotnetrocks

  • Jun 21st, 2006
 

Performance wise both the approaches are similiar. BUt consider this statement. suppose some object obj has a variable in it as i. then, obj.1 = obj.i + 1In this case at the MSIL level u can see 2 diff calls to the obj.get and obj.set property. There are 2 ldloc instruction calls in the MSIL..While if we had used obj.i += 1 then there is just one call to the obj.get property which is something better in performance if the property did many time consuming validations. Thus i think guys as far as possible try using these compount operators like +=, -= etc. Thanksdotnetrocks

  Was this answer useful?  Yes

samiksc

  • Jul 11th, 2007
 

x=x+1 is equivalent to x+=1 except that value of x is evaluated only once.
x=x+1 is not equal to x++ (not always equal as mentioned in one of the earlier comments). If x++ is a standalone statement, the two have equal effect.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions