J
joels
I'm a bit confused by the behavior of arrays and exactly when a
shallow vs. deep copy is made.
When passing an array of value types (e.g., int), msdn states that a
deep copy is NOT made, thus, one would infer that this works exactly
like the Clone method of the array class; i.e., creating a shallow
copy.
When calling the Clone method on an array, the cloned copy is a
shallow copy of the original, thus, the new (cloned) array should
refer to the same set of values, yes? However, this is not the case
when the array contains value types.
For example, if I'm passing an array to a method and increment the
first item in the array, the original array reflects that change.
HOWEVER, if I clone the array, and then increment the first item in
the reference which holds the cloned array, the original does NOT
reflect the change-- unlike the change noticed when passing an array
as a parameter.
HOWEVER, this all changes when the array holds object references, in
which case changes to the array reference holding the cloned array are
reflected in the original.
I'm looking for the internals workings here, I hope I was clear in my
discussion.
Thanks for any help,
Joel
shallow vs. deep copy is made.
When passing an array of value types (e.g., int), msdn states that a
deep copy is NOT made, thus, one would infer that this works exactly
like the Clone method of the array class; i.e., creating a shallow
copy.
When calling the Clone method on an array, the cloned copy is a
shallow copy of the original, thus, the new (cloned) array should
refer to the same set of values, yes? However, this is not the case
when the array contains value types.
For example, if I'm passing an array to a method and increment the
first item in the array, the original array reflects that change.
HOWEVER, if I clone the array, and then increment the first item in
the reference which holds the cloned array, the original does NOT
reflect the change-- unlike the change noticed when passing an array
as a parameter.
HOWEVER, this all changes when the array holds object references, in
which case changes to the array reference holding the cloned array are
reflected in the original.
I'm looking for the internals workings here, I hope I was clear in my
discussion.
Thanks for any help,
Joel