Datatable in using?

I declared a datatable internally using a block that calls the Dispose method at the end of the scope.

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

But in the reflector datatable does not have Dispose function

enter image description here

Like this?

+6
source share
3 answers

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

The reflector will not display methods of the base type unless they are overridden in the derived type.

+3
source

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

 public class DataTable : MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISupportInitialize, ISerializable, IXmlSerializable public class MarshalByValueComponent : IComponent, IDisposable, IServiceProvider 

Your code block will be presented under the hood, as shown below, so it ensures that the Dispose () method is called:

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

See MSDN for details: Using Statement

+3
source

Why are you trying to get rid of DataTable? You must remove it from your DataSet if you really want this to happen.

-1
source

Source: https://habr.com/ru/post/902624/


All Articles