Heinz Kiosk <(E-Mail Removed)> wrote:
> A piece of code generated by an MS wizard goes as follows:
>
> public class MyDataTable : DataTable...
> {
> ...
> public override DataTable Clone()
> {
> MyDataTable cln = ((MyDataTable)(base.Clone())); // What is going on here?
> cln.InitVars();
> return cln;
> }
> ...
> }
>
> MyDataTable is derived from DataTable. How does casting a DataTable to a
> MyDataTable in the commented line work? I must be missing some rules about
> how casting works in .NET. If you cast to a derived class does the compiler
> fill in defaults for derived members or use the derived constructor and then
> do a field-by-field copy or what? Something clever must be going on.
base.Clone() is actually returning a reference to a MyDataTable object
- at least if it's working according to the documentation. It's
probably using object.MemberwiseCopy() - have a look at the description
for that method for more information. Hope that explains what's going
on - let me know if you have any more concerns.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too