Several methods of C # copy array

Source: Internet
Author: User
Suddenly learned, so put on the blog to share, right when it is a study diary it.

First of all, the array is a reference type, so be careful not to copy the address when copying and not copy the value Oh!

In fact, when copying arrays, be sure to use new in the heap to open a new space dedicated to storing arrays, so that is effective.

(1)

Int[] Pins = {9, 3, 7, 2}; Int[] Copy=new int[pins.length];      for (int i = 0; i < copy.length; i++)      {        Copy[i] = pins[i];    }

(2)

int[] copy = new int[pins. Length]; Pins. CopyTo (copy, 0);

(3)

Int[] pins= new int[4]{9,3,7,2}; Int[] Alias=pins;

Note that this copy is just a reference, but the address of the data is passed to the alias array, so it is not recommended to copy the array in this way;

(4)

Array.copy (pins,copy,copy. Length)

(5)

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

This explains why the coercion type conversion of int[] is used because the result type of clone is object, so it needs to be cast to int[]

The object class is actually the base class for all of our classes.

The above this C # copy array of several methods (summary) is the small part to share the whole content of everyone, hope to give you a reference, but also hope that we support topic.alibabacloud.com.

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.