C # how to copy array elements

Source: Internet
Author: User

Because the array is of the reference type, assigning a value using the array1 = array2 method will direct array1 and array2 to the same memory space. Changing the elements of array1 and array2 will modify the same memory space.

However, we often want the replication effect. We only want the elements of array1 to be the same as those of array2. Then we can use a loop to make array1 [I] = array2 [I] one by one.

Or use the copyto method to copy:

Array2.copyto (array1, 0); // copy all the contents of the array2 array to array1 and insert it from element 0th of array1.

 

By the wayString Transmission Method:

String is also a reference type, but a new space is automatically created when the string value is assigned.

For example, string a = '"123 ";

String B =;

In this case, the address of B is different from the address of a. the compiler automatically creates a new space for B and copies the content of a to the space of B. This is not the same as the simple reference transfer method for arrays. It is special for strings.

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.