Saturday, August 25, 2007

Array > Copy and CopyTo Methods

EXPLORING Array.Copy AND Array.CopyTo METHODS
• System.Array class provides the methods to copy arrays: Copy, CopyTo, Clone
• CopyTo method copies the contents of one array to another, given a specified starting index
• Array.Copy is used as under:
int[] pins = {4,9,3,7};
int[] copy = new int[pins.Length];
pins.CopyTo(copy, 0);
• Copy method requires the target array to be initialized before the Copy call is made
• Use of Copy shown as under:
int[] pins = {4,9,3,7};
int[] copy = new int[pins.Length];
Array.Copy(pins, copy, copy.Length);
• Another method called Clone, allows to create a new array and copy it in one action
• Use of Clone is shown as under:
int[] pins = {4,9,3,7};
int[] copy = (int[])pins.Clone();

No comments: