Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 758
Royi Namir Avatar asked Nov 29 '11 08:11

Royi Namir


2 Answers

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.

like image 198
tafa Avatar answered Oct 03 '22 12:10

tafa


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

like image 31
sll Avatar answered Oct 03 '22 10:10

sll