Why is this example of code behaving differently in c++ and C#.
[C++ Example]
int arr[2];
int index = 0;
arr[index]
index in C# is a value type, which means you return a new instance of the value when you perform operations on it.
If you imagine it as a procedure instead of an operator, the procedure would look like this:
public int Increment(int value)
{
int returnValue=value+1;
return returnValue;
}
C++, however, works on the reference of the object, so the procedure would look like:
int Increment(int &value)
{
value=value+1;
return value;
}
Note: if you had been applying the operator on an object (say overloaded the ++ operator) then C# would behave like C++, since object types are passed as references.