Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Both the Copy and the Clone methods create a new DataTable with the same structure as the original DataTable.
Clone method can not create datarow but Copy method create both structure and datarow. Why should we use Clone method instead of Copy method. please explain me. Advance thanks for helping me.
Posted
Updated 18-Sep-13 19:37pm
v2

Let's see:

C#
class A {
   internal A A; // non-private fields is the bad coding style, but this is just for example
   internal Name;
} //A

A a = new A();
A b = a; // a and b reference the same object:
// if you change a.Name, b.Name will change

b = a.MemberwiseClone();
// not a and b are distinctly different objects
// they can have different names:
a.Name = "This is a";
b.Name = "This is b"; // different, you can check it up after this statement.

// Now, how about a.A?
// After cloning, it still references the same object, because A is a reference type:
A a = new A();
a.A = new A();
a.A.Name = "inner";

b = a.MemberwiseClone();
b.A.Name = "another inner";
// it will change a.A.Name! Why?
// because we cloned a, but not a.A


Now, look at the last comment in the code sample shown above. It means that cloning is not deep.
See also:
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^],
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx[^],
http://en.wikipedia.org/wiki/Clone_%28computing%29[^].

Finally, please see my recent answers on deep cloning: How to implement ICloneable for a List Class?[^].

—SA
 
Share this answer
 
Comments
ridoy 19-Sep-13 3:02am    
another classic,5ed!
Sergey Alexandrovich Kryukov 19-Sep-13 10:54am    
Thank you very much.
—SA
The difference is very straight forward.

DataTable.Clone method clones the structure of the dataTable, including all dataTable schemas and constraints. Not data.

DataTable.Copy method copies both the structure and data.

Regards..
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Sep-13 1:49am    
(Sigh...) This is not clear, really. How would you explain the difference between "structure" and "data"? and what is "structure"? Not clear at all.

Please see my answer.

—SA
Praveen2886 19-Sep-13 7:31am    
This solution is clear for all .net professions . if u want to know the difference jus google it.
Sergey Alexandrovich Kryukov 19-Sep-13 11:56am    
True, but if OP asks the question, it may be not clear to him. If he could understand it all just by a Web search, none of our answers would be needed at all. Isn't that logical?
—SA
Sumon562 19-Sep-13 2:15am    
If I get the structure and data simultaneously from Copy method then why should I use clone.
Isn't it a wast of time, or it consume time when compile?
Praveen2886 19-Sep-13 7:34am    
it depends on ur situation to use clone and copy. if u want the same structure(Dont ask me wat is structure) but u need to add user defined data, clone ll be handy
 
Share this answer
 
"Both the Copy and the Clone methods create a new DataTable with the same structure as the original DataTable. The new DataTable created by the Copy method has the same set of DataRows as the original table, but the new DataTable created by the Clone method does not contain any DataRows."

From DataTable.Clone Method documentation. In summary, Clone method creates a new DataTable with same structure and constraints but without data and Copy method does same but includes data too.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/10d99d29-2851-4f12-84b6-d95e08c13bf4/whats-the-difference-between-clone-and-a-copy[^]


http://stackoverflow.com/questions/198496/difference-between-the-system-array-copyto-and-system-array-clone[^]
This is ther main difference


1- CopyTo require to have a destination array when Clone return a new array.<br />
2- CopyTo let you specify an index (if required) to the destination array.


I think you will find difference now....:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900