What is the reason of the negative sign?
You have a negative sign, because you have exceeded the maximum integer value and the next integer is the lowest integer that can be represented.
Why I don't have a compilation or runtime error?
You don't have a complilation error, because this is not an error. Also, this is not a runtime error. You just add one to the i
in the runtime. Since the value of i
is the maximum integer value that can be stored in a variable of type int
and since the circular nature of the integers in programming, you will get the lowest integer that can be stored in a variable of type int
.
(A variable of type int
can store 32-bit integers).
Furthermore, by default you in C# integer operation don't throw exceptions upon overflow. You could alter this either from project settings or using a checked
statement, as it is already have been pointed out here.