Copy vs Clone (DataSet)

I was looking at the DataSet object the other day and realized that there is both a Copy and a Clone methods. They are designed to fulfil similar functions which the following code highlights.

The Clone method is designed to only copy the structure i.e. the schema, relations and constraints.

The Copy method is designed to replicate the structure and the associated data.

DataSet data1 = new DataSet();
DataSet data2 = new DataSet();

System.IO.FileStream fs = new System.IO.FileStream(@"C:\test\test.xml", System.IO.FileMode.Open);
data1.ReadXml(fs);

data2 = data1.Clone();
foreach (DataRow dr in data2.Tables[1].Rows)
{
    Console.WriteLine(dr[0].ToString()); // We called the Clone method
}

 

data2 = data1.Copy();
foreach (DataRow dr in data2.Tables[1].Rows)
{
    Console.WriteLine(dr[0].ToString());
}

 

Technorati tags: , ,


Comment Section

Comments are closed.