Here's where .NET starts to do my head in. Consider the following scenario:
- I have a
DataTable
object, containing a number ofDataRow
objects - I add some of the
DataRow
objects to an Array - I call
Dispose
on theDataTable
.
What guarantee do I have that calling Dispose
on the DataTable
won't compromise the DataRow
objects (which, by reference, would affect those added to the Array)?
MS documentation is zero help in this case.
EDIT: So far folks have missed the point entirely. IDisposable
objects are meant to be disposed of as soon as possible. My code is going to be creating thousands of DataTable
objects during a typical day. No way am I leaving those un-disposed if they implement IDispoable
.
BUT, I need the handful of DataRow
objects to stay alive because that's what my 3rd-party binary (which I have no control over) expects. And I need them to stay alive for significantly longer than the DataTable
objects.
All I need is authoritative information that describes what Dispose
on a DataTable
actually does. Looks like said information does not exist, which is why I'm asking here for help. Someone may have a reference, or documentation I have overlooked, that answers my need.
EDIT 2: I have tested as Blam suggested, including adding GC.Collect
+ a bonus GC.WaitForPendingFinalizers
+ one more GC.Collect
after disposing the DataTable
. The DataRows
appear to be fine, still alive and accessible. That's positive, but alas not authoritative enough to include in production code :-(
DataTable
doesn't re-implementMarshalByValueComponent.Dispose
, and that method doesn't really do much on its own. So in effect, callingDataTable.Dispose
doesn't do anything to the table or the data it contains. Interestingly enough,DataTable
even goes so far to suppress finalization in the constructor, because it doesn't hold unmanaged resources.Now, if you look at the definition for
DataRow
, you'll notice that it doesn't implementIDisposable
(or anything for that matter), doesn't have a finalizer, etc. It does, however, have a dependency on the table (which it gets via the DataRowBuilder passed in the constructor). Normally in that sort of situation, you never want to call dispose on an owning object until absolutely no one needs it anymore, as it could result in side effects from that parent reference being disposed of already.However, in this case calling dispose doesn't actually do anything and it doesn't matter either way. In fact, you're calling
Dispose
as a premature optimization that has little or no effect, so it's superfluous. YourDataRow
s and the data they contain will be unaffected by calling dispose on the table.(Sorry but not sorry the example is C# and not VB.NET, but there is no difference between the two in this example, except one is uglier than the other ;p )
I don't know what constitutes as authoritative enough for production code, but there you go.