I pass a two-dimensional array as a property to my user control. There I store this values in another two-dimensional array:
int[,] originalValues = this.Met
You need to create a new array. You then need to manually copy the value of each element into the new array. What you are doing in the example given is to create two array variables which both reference the same array.
The problem with the clone method is that it is a shallow copy. In this isntance, because you are using int
, it does not mater. Howver, if you had an array of classes the definition of the ICLonable interface leaves it ambiguous as to how deep the clone will go.
Imagine if you had a class that has properties that are other classes which has properties that are other classes. The clonable interface does not state whether it will also clone the sub members or not. Moreover, many people have different views on what the expected behaviour is.
Hence this is why it is often recommended to define two interfaces, IShallowCopy
and IDeepCopy
.