You have two issues here.
First, the 'before/after return' issue means you are seeing two different execution frames-- that is, in the debugger the stack trace will show a bunch of PrintReverse
s on top of each other because each is there in its own context, with its own state, concurrently. It's almost (though not really) like an 'instance' of that method, and you're seeing two different ones.
Second, because each has its own state, the local variables in each-- including, critically, the parameters-- are also duplicated. They initially point to the same heap object (your initial Char array), but they are all different variables.
Now, look at this code:
char[] test1 = new char[] { '1', '2', '3' }, test2 = test1;
Array.Resize(ref test2, 2);
MessageBox.Show(new string(test1) + " - " + new string(test2)); // result: 123 - 12
If you run this, you'll see that although the variables initially refer to the same object, Array.Resize creates a new object and changes the reference of the variable passed in to point to the new one. The reference in the first variable remains pointing at the old (immutable) object.
This is what's happening in your case, only with the Chars parameter. In each method, you reassign Chars to point elsewhere using Array.Resize(), but the original variables remain referencing the old location.
str.ToArray()
instead ofstr.ToCharArray()
. – Jon Senchyna Jun 13 '12 at 15:38