operators precedence<p>why is that i the operators precedence of c# its seems like (x++) will be execute before (++x)? is it true?</p> <p>which is the highest operator precedence here --&gt; (++x),(x++),&lt;&lt;    ?</p> <p>thanks in advanced</p> <p> </p>© 2009 Microsoft Corporation. Tous droits réservés.Tue, 15 Sep 2009 13:30:21 Z02d3486c-a4ac-4988-8a25-05f7be86d590http://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#02d3486c-a4ac-4988-8a25-05f7be86d590http://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#02d3486c-a4ac-4988-8a25-05f7be86d590CoverPplhttp://social.msdn.microsoft.com/Profile/fr-FR/?user=CoverPploperators precedence<p>why is that i the operators precedence of c# its seems like (x++) will be execute before (++x)? is it true?</p> <p>which is the highest operator precedence here --&gt; (++x),(x++),&lt;&lt;    ?</p> <p>thanks in advanced</p> <p> </p>Wed, 11 Oct 2006 16:18:04 Z2006-10-11T16:18:04Zhttp://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#e49a820f-a2f1-439e-854f-556417ba0acbhttp://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#e49a820f-a2f1-439e-854f-556417ba0acbMark Rendlehttp://social.msdn.microsoft.com/Profile/fr-FR/?user=Mark%20Rendleoperators precedence<p>++x increments x and then &quot;returns&quot; its value.</p> <p><font face=Verdana>x++ &quot;returns&quot; the value and then increments x</font></p> <p>So</p> <p><font face="Courier New, Courier, Monospace">int x = 0;<br>Console.WriteLine(x++); // Outputs 0<br>Console.WriteLine(x); // Outputs 1</font></p> <p><font face="Courier New, Courier, Monospace">x = 0;<br>Console.WriteLine(++x); // Outputs 1<br>Console.WriteLine(x); // Outputs 1</font></p>Wed, 11 Oct 2006 16:25:55 Z2006-10-11T16:25:55Zhttp://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#882ebc3d-2e7a-4982-b6db-e2d19d69622fhttp://social.msdn.microsoft.com/Forums/fr-FR/csharplanguage/thread/02d3486c-a4ac-4988-8a25-05f7be86d590#882ebc3d-2e7a-4982-b6db-e2d19d69622fTaylorMichaelLhttp://social.msdn.microsoft.com/Profile/fr-FR/?user=TaylorMichaelLoperators precedence<P>As Mark said the operators perform two different tasks so oftentimes this accounts for the behavioral problems that people see.&nbsp; If you are ever concerned about the precedence of operators you should probably refactor your code (by adding parenthesis) as others will be confused to.</P> <P>As for precedence postfix increment and decrement (x++, x--) are higher precedence than prefix (++x,--x).&nbsp; They are at the same level as function calls, array indexing, field referencing and a few other operators.&nbsp; Prefix operators are with the unary plus and minus operators, negation, not and typecasts.&nbsp; Again, if you are unsure of the precedence then use parenthesis to make it clear.</P> <P>As for the original question as to why this is it is because the C# designers wanted to follow the rules of C++.&nbsp; Why did the C++ designers set it this way?&nbsp; Because they wanted to follow C.&nbsp; Why did the C designers set it this way?&nbsp; Technically they didn't.&nbsp; The C (or C++) standards leave the order of operator evaluation as undefined meaning that each compiler can evaluate expressions in whatever order they want.&nbsp; </P> <P>Now before anybody starts pointing to precedence charts in their favorite C/C++ book please take a moment to look at the C or C++ standards.&nbsp; Precedence isn't defined other than as how the topics are covered in the spec.&nbsp; However the specs do specify the grammars that must compile.&nbsp; It is in these grammar rules where operator precedence is set up.&nbsp; Way down deep in an expression rule you'll find that postfix expressions are evaluated before prefix expressions.&nbsp; This means they have higher precedence.&nbsp; Therefore, unlike some languages, C/C++ define the operator precedence in the grammar rules rather than as specific hierarchies in the specification.&nbsp; As a result you can build a precedence table given the grammar rules.&nbsp; Why exactly the C designers felt postfix was higher than prefix I don't know.</P> <P>Michael Taylor - 10/11/06</P>Wed, 11 Oct 2006 16:32:20 Z2006-10-11T16:32:20Z