[Reserved] the difference between C # DataTable class method Clone and Copy

In C #, the Datatable class, Clone method and Copy method can be used to copy the current DataTable object, but the Clone method of DataTable class and Copy method is still a difference, Clone method to copy only the structure information, including all DataTable architecture and constraint, but in addition to the copy method to copy the configuration information, also copy data table row the DataTable.

DataTable.Clone Method: Cloning DataTable structure, including all DataTable schema and constraints.

DataTable.Copy Method: copying the data structure and DataTable.

Method for Copy and the Clone method to DataTable class, for example as follows:

            DataTable dataDt = new DataTable();

            dataDt.Columns.Add(new DataColumn() { ColumnName = "Name" });
            dataDt.Columns.Add(new DataColumn() { ColumnName = "Id" });
            dataDt.Columns.Add(new DataColumn() { ColumnName = "Memo", DataType=typeof(String) });


            DataRow newRow = dataDt.NewRow();
            newRow["Name"] = "李四";
            newRow["Id"] = 22;
            newRow["Memo"] = "后续新增";
            dataDt.Rows.Add(newRow);  

            DataRow inserDataRow = dataDt.NewRow();
            inserDataRow["Name"] = "王五";
            inserDataRow["Id"] = 23;
            inserDataRow [ "Memo"] = "first line";
            dataDt.Rows.InsertAt(inserDataRow, 0);


            var newDt1 = dataDt.Clone (); 
            var newDt2 = dataDt.Copy ();

From the results, running, and configuration information newDt1 consistent newDt2 no difference, but the data of 0 newDt1 behavior, data behavior newDt2 and 2, and in accordance with the data newDt2 dataDt table data.

 

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link for the difference _IT technology of C # and Clone Copy method DataTable class of small fun house .

Bloggers personal technology exchange group: 960 640 092, blogger micro-channel public numbers are as follows:

Guess you like

Origin www.cnblogs.com/xu-yi/p/11246477.html