Why is this example of code behaving differently in c++ and C#.
[C++ Example]
int arr[2];
int index = 0;
arr[index]
In the case of C++, at least, you're invoking undefined behavior by preincrementing and using index
without a sequence point in between. If you feed that code to GCC with warnings enabled it will say:
preinc.cpp:6: warning: operation on ‘index’ may be undefined
I'm guessing that it's undefined as well in C#, but I don't know the language. For C and C++ at the very least though, the answer is that the compiler can do anything it wants without being wrong because your code is erroneous. There's no obligation for different compilers (or even the same compiler) to produce consistent results, either.