The difference between the shallow copy clone () and copy () of the C # array class

Source: Internet
Author: User

1 Array.clone method

namespaces: System

Assembly: mscorlib

Syntax:

Public Object Clone ()

a shallow copy of an array copies only the elements of an array, whether they are reference types or value types, but is not responsible for the objects referenced by those references.

The reference in the new array points to the same object as the reference to the original array.

Example:

int[] IntArray1 = {1, 2};

int [] intArray2 = (int []) intarray1.clone ();

It is necessary to note that the coercion type conversion is required because the type returned by Clone () is Object

2 Array.copy Method

namespaces: System

Assembly: mscorlib

copy has several overloaded functions:

//Start by copying a series of elements in an array from the first element, pasting them into another array (starting with the first element). Length is a 32-bit integer

Public static void Copy (array sourcearray, array destinationarray, int length)

//Start by copying a series of elements in an array from the first element, pasting them into another array (starting with the first element). Length is a 64-bit integer

Public static void Copy (array sourcearray, array destinationarray, long length)

//start with the specified source index, copy a series of elements from the array, paste them into another array (starting at the specified target index). Length and index specified as 32-bit integers

Public static void Copy (Array sourcearray, int sourceindex,array destinationarray, int destinationindex,int length) /c3>

//start with the specified source index, copy a series of elements from the array, paste them into another array (starting at the specified target index). Length and index specified as 64-bit integers

Public static void Copy (Array sourcearray, Long Sourceindex,array destinationarray, long Destinationindex,long Length)

Example: Array myintarray=array.createinstance (typeof (System.Int32), 5);

for (int i = myintarray.getlowerbound (0); I <= myintarray.getupperbound (0); i++)

{myintarray.setvalue (i+1, i);}

Array Myobjarray = Array.CreateInstance (typeof (System.Object), 5);

for (int i = myobjarray.getlowerbound (0); I <= myobjarray.getupperbound (0); i++)

{myobjarray.setvalue (i+26, i);}

//Copies The first element from the Int32 array to the Object array.

array.copy (Myintarray, Myintarray.getlowerbound (0), Myobjarray, Myobjarray.getlowerbound (0), 1);

//Copies The last of the elements from the Object array to the INT32 array.

array.copy (Myobjarray, Myobjarray.getupperbound (0)-1, Myintarray, Myintarray.getupperbound (0)-1, 2);

difference:

Clone () return value is object,copy return value is void

Clone () is a non-static method, and copy is a static method.

Clone () creates a new array, and the copy method must pass an existing array with the same order and sufficient elements.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between the shallow copy clone () and copy () of the C # array class

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.