Several methods of C # copy array

Source: Internet
Author: User

The known quantity group is as follows:

Int[] Array = {1, 5, 9, 3, 7, 2, 8, 6, 4};

(1). Reference replication, easy to cause errors, not recommended

int[] Copy = array;

(2). Traverse Copy

int[] copy = new Int[array. Length];

for (int i = 0; i < array.length; i++)
{
Copy[i] = Array[i];
}

(3). Using the CopyTo method

int[] copy = new Int[array. Length];

Array. CopyTo (copy, 0);

The CopyTo method is used as a copy of the source array to the destination array, you can specify the starting index of the target array, but you need to ensure that the destination array can hold the source array, CopyTo can be used to merge multiple arrays

(4). Using the Array.copy method

int[] copy = new Int[array. Length];

Array.copy (array, Copy, array. Length);

The Array.copy method can copy some elements of the source array into the target array, three parameters, you can specify the number of elements copied from the source array (starting with the first element), and the five parameters can specify the number of elements copied from the source array and the starting index, or the starting index of the target array.

(5). Using the Clone method

Int[] copy= (int[]) array. Clone ();

Because the return value type of clone is object, cast to int[]

Several methods of C # copy array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.