Royi Namir
Royi Namir

Reputation: 148524

Datatable inside using?

I have declared datatable inside using block which calls the Dispose method at the end of the scope.

 using (DataTable dt = Admin_User_Functions.Admin_KitItems_GetItems())
            {
                 ...
            }

But in reflector, datatable doesnt seens to have Dispose function

enter image description here

How is that ?

Upvotes: 6

Views: 868

Answers (3)

tafa
tafa

Reputation: 7326

System.Data.DataTable extends System.ComponentModel.MarshalByValueComponent and, MarshalByValueComponent implements IDisposable.

Reflector would not display the methods of the base type unless they are overriden in the derived type.

Upvotes: 3

sll
sll

Reputation: 62504

DataTable inherited from MarshalByValueComponent class which implements IDisposable interface (see below), C# allows calling base class public methods for the instances of derived classes.

public class DataTable : MarshalByValueComponent, 
    IListSource, ISupportInitializeNotification, 
    ISupportInitialize, ISerializable, IXmlSerializable

public class MarshalByValueComponent : 
    IComponent, IDisposable, IServiceProvider

Your code block would be represented under the hood as shown below, so it guarantee that Dispose() method will be called:

{
  DataTable dt = Admin_User_Functions.Admin_KitItems_GetItems()

  try
  {
     // .. code inside using statement
  }
  finally
  {
    if (dt != null)
      ((IDisposable)dt).Dispose();
  }
}

See MSDN for more details: using Statement

Upvotes: 3

zmbq
zmbq

Reputation: 39013

Why are you trying to dispose of the DataTable? You should delete it from its DataSet if you really want this to happen.

Upvotes: -1

Related Questions