C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Differrence between Array.CopyTo and Array.Clone


Posted Date: 12 Oct 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Uvarajan       Member Level: Silver
Rating:     Points: 10



Both CopyTo() and Clone() make shallow copy (data is copied) and used for Single Dimension arrays.

Clone() method makes a clone of the original array. It returns an exact length array.

CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array.

In VB.NET, variables has to be declared earlier, before using Clone or CopyTo methods to store the values. The difference arise on mentioning the size of the array.

While declaring the varaiable that is used for CopyTo method, it should have the size equal to or more than that of the original array.

While declaring the variable that is used for Clone method we need not mention the array size. Eventhough the array size is small, it won't show any error, rather the array size gets altered automatically on cloning. The cloned array size get created equal to the size of the source array.

If value type is used for CopyTo/Clone any change in the values made on them will not get reflected on the original whereas on reference type the change gets reflected.

Below is the code which would throw some light on your understanding.


Dim mOriginal(2), mCopy(5), mClone() As Integer

'Storing values into Original Array
mOriginal(0) = 5
mOriginal(1) = 8
MessageBox.Show(mOriginal(0), "Original")
MessageBox.Show(mOriginal(1), "Original")

'Copy the array and modify the values in copied array
mOriginal.CopyTo(mCopy, 0)
MessageBox.Show(mCopy(0), "Copy")
MessageBox.Show(mCopy(1), "Copy")
mCopy(0) = 15
mCopy(1) = 18
MessageBox.Show(mCopy(0), "Copy")
MessageBox.Show(mCopy(1), "Copy")
MessageBox.Show(mOriginal(0), "Original")
MessageBox.Show(mOriginal(1), "Original")

'Clone the array and modify the values in cloned array
mClone = mOriginal.Clone()
MessageBox.Show(mClone(0), "Clone")
MessageBox.Show(mClone(1), "Clone")
MessageBox.Show(mClone.Count, "Size of Clone Array")
mClone(0) = 25
mClone(1) = 28
MessageBox.Show(mClone(0), "Clone")
MessageBox.Show(mClone(1), "Clone")
MessageBox.Show(mOriginal(0), "Original")
MessageBox.Show(mOriginal(1), "Original")





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Array.CopyTo and Array.Clone  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Use Of Generics In Dot Net Frame
Previous Resource: Boxing and Unboxing in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

accuconference

Contact Us    Privacy Policy    Terms Of Use