How to make a shallow copy of an array?

You can clone an array, which makes a copy of it:

int[,] originalValues = (int[,])this.Metrics.Clone();

I don't know where I got this from, but this works well for me.

public static class GenericCopier<T>    //deep copy a list
    {
        public static T DeepCopy(object objectToCopy)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, objectToCopy);
                memoryStream.Seek(0, SeekOrigin.Begin);
                return (T)binaryFormatter.Deserialize(memoryStream);
            }
        }
    }

Tags:

C#

.Net