When calling the increment operator, the object is assigned a new reference, that reference has the new values.
Example. We have a class IntWrapper
public class IntWrapper
{
public int IntValue
{
get;
set;
}
}
and we want to use it like this
IntWrapper a = new IntWrapper();
IntWrapper b = a;
a++;
Console.WriteLine("a = {0}, b = {1}", a.IntValue, b.IntValue);
Here are two possible implementations for the increment operator overload in our class, the second is the correct way
public static IntWrapper operator ++(IntWrapper instance)
{
instance.IntValue++;
return instance;
}
public static IntWrapper operator ++(IntWrapper instance)
{
IntWrapper result = new IntWrapper();
result.IntValue = instance.IntValue + 1;
return result;
}