Ordering matters

suggest change

There is overflow in the following code

int x = int.MaxValue;
Console.WriteLine(x + x + 1L);  //prints -1

Whereas in the following code there is no overflow

int x = int.MaxValue;
Console.WriteLine(x + 1L + x);  //prints 4294967295

This is due to the left-to-right ordering of the operations. In the first code fragment x + x overflows and after that it becomes a long. On the other hand x + 1L becomes long and after that x is added to this value.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Overflow:
* Ordering matters

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
79 Overflow
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script