By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,951 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,951 IT Pros & Developers. It's quick & easy.

Deep/Shallow copy when returning objects (DataTable) from methods

P: n/a
Should I be concerned about the classic "Deep/Shallow" copy problem
when returning objects, specifically a DataTable or DataView from a
method? For example, see the code below:

private DataTable LoadStuff()
{
DataTable dt = new DataTable();
....
dt.Columns.Add(...)
....
dt.Rows.Add(...)

return dt;
}

Will the memory allocated within this function be valid outside of the
function after the return? Wouldn't the DataTable object need to
implement the ICloneable interface to perform a deep copy?

By contrast, how does this differ if I was using returning my own user
defined object instead the DataTable?

Thanks!
Andy

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
bubby <aj******@yahoo.com> wrote:
Should I be concerned about the classic "Deep/Shallow" copy problem
when returning objects, specifically a DataTable or DataView from a
method? For example, see the code below:

private DataTable LoadStuff()
{
DataTable dt = new DataTable();
....
dt.Columns.Add(...)
....
dt.Rows.Add(...)

return dt;
}

Will the memory allocated within this function be valid outside of the
function after the return? Wouldn't the DataTable object need to
implement the ICloneable interface to perform a deep copy?

By contrast, how does this differ if I was using returning my own user
defined object instead the DataTable?


It wouldn't at all. You really need to get to grips with how reference
types and value types work, and how garbage collection works, before
you dive into things like ADO.NET.

See http://www.pobox.com/~skeet/csharp/parameters.html for details
about reference types etc. See
http://msdn.microsoft.com/msdnmag/is...I/default.aspx
for information about garbage collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.