According to http://msdn.microsoft.com/en-us/library/system.data.datatable.clone.aspx “If these classes have been derived, the clone will also be of the same derived classes”.
In the example code below how does the clone method on the base class “know” to create the derived class?
public class MyDataTable : System.Data.DataTable
{
public override System.Data.DataTable Clone()
{
//How is this a MyDataTable not a "DataTable" ?
return base.Clone();
}
}
static void Main(string[] args)
{
MyDataTable dt = new MyDataTable();
System.Diagnostics.Debug.Assert(dt.Clone() is MyDataTable);
}
If you dig through the code for DataTable with Reflector, you’ll encounter this method, called by the private Clone(DataSet) method:
In other words, it creates a new instance of your class, not DataTable. Be careful, this clone will have a deep copy of the typical DataTable properties but any fields you might have added but do not assign in your constructor will have their default values.