Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
2.00/5 (5 votes)
See more:
  int a = 10;
int c = a++ + a++ + ++a;
MessageBox.Show(a.ToString());
MessageBox.Show(c.ToString());


  int a = 10;
int c = a++ + a++ - ++a;
MessageBox.Show(a.ToString());
MessageBox.Show(c.ToString());


What I have tried:

Hi All,
After creating Above Program Of Pre Increment And Post Increment,
Both Code Have Different Solution As Per C++ Concept.
Please Give The Proper Concept For C#.
Posted
Updated 2-Feb-17 5:38am
v3
Comments
Philippe Mori 2-Feb-17 8:31am    
You should have written what it is displayed.

The simple answer is: use those increment operators as single statements only, never combine them with other statements! That won't slow down your program at all, the compiler will have to create the extra variables anyway. The only thing you cause with such combinations is: confusion.
 
Share this answer
 
Comments
Philippe Mori 2-Feb-17 8:29am    
Well, the rules for C# are well defined but it still a bad idea. See my answer.
Don't do it.
Have a look at this answer: How the expression works c= ++a + ++a;[^]
 
Share this answer
 
Comments
Maciej Los 2-Feb-17 2:47am    
No, it wouldn't be so easy ;)
Philippe Mori 2-Feb-17 8:28am    
Well, the rules for C# are well defined but it still a bad idea. See my answer.
OriginalGriff 2-Feb-17 8:39am    
That's why I said "Don't do it". :laugh:
You are in gray zone. In C/C++/C# definitions, it is stated that the compiler is allowed to change the order of elements in an expression (and more), and since your expression depend on the order of operations, the result is not predictable. Another compiler will give different results.

Use a debugger and display assembly code generated to see how your code was translated.
 
Share this answer
 
Comments
Philippe Mori 2-Feb-17 8:28am    
Well, the rules for C# are well defined but it still a bad idea. See my answer.
Well in C# rules are more defined than in C++...

Increment in C#, C and C++[^]

In C#, you first example will show 13 and 34 and your second example will show 13 and 8.

In C++, it would be undefined behavior. Both the value of a and c would be indetermined. In practice, the value will depend on the order the compiler will do operations.

C/C++ compiler can essentially evaluate the 3 sub expression in any order and can save back the result of postfix operator at any time and it might load variables value from memory once or multiple times. Thus as soon as the same variable is modified more than once in an expression without sequence point between the 2 operations, the result would be undefined.

In practice a would probably be 11, 12 or 13 in first example and c between 31 and 34 as undefined behavior is usually one of the possible interpretation. For the second example c would probably be between 7 and 12.

By the way, in C/C++ the value will depend on the compiler and often it might also depends on the optimization.

Update
Some more information for C++: Sequence point - Wikipedia[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900