The Array
class in the System
namespace provides the Clone()
method, which can be used to do a shallow copy of the array elements.
public object Clone ();
In the below code, we have created and initialized an integer array. We first print the original array and then perfrom a shallow copy of the array using the Array.Clone()
method.
After that, we print the cloned array contents.
The program prints the below output and exits.
Original Array : 10,13,15,18,23
Cloned Array : 10,13,15,18,23
using System;class ArrayClone{static void Main(){int[] numArray = {10,13,15,18,23};Console.WriteLine("Original Array : {0}", string.Join(",",numArray));int[] clonedNumArray = (int[])numArray.Clone();Console.WriteLine("Cloned Array : {0}", string.Join(",",clonedNumArray));}}