. The comparison of Clear () has no meaning, because it is only to empty the DataTable, in the heap to allocate memory, the general comparison is also more close () method, but the DataTable does not have this method
The difference between Dispose and null is interesting.
First of all
DataTable dt = new DataTable ();
You know DT on the stack, the new DataTable () object pointing to the managed heap
and dt = null, which means that DT does not point to any object, and the new DataTable () is still in memory, waiting for GC to be recycled (when is the garbage collection mechanism?)
and dt. Dispose () means the destruction of the new DataTable (); This object, but DT still points to the address of the object being destroyed; DT is not NULL at this time, but it cannot be used
You can look at the results of the following code
DataTable dt = new DataTable ();
Dt. Dispose ();
MessageBox.Show (dt = = null). ToString ());
In actual development, it is sufficient to set the DT to NULL, which can wait for GC to be recycled. If you want to use the Dispose () method, it is generally
using (DataTable dt = new DataTable ())
{
}
This implicitly calls the Dispose () method at the end of the curly brace, and DT becomes a local variable immediately out of scope, and there is no call error
===============================================================
How to take the initiative to recycle the =null?
===============================================================
Gc. Collect ();
All objects are considered recycled regardless of how long the object is in memory, but objects referenced in managed code are not recycled. Use this method to force the system to attempt to reclaim the maximum amount of memory available.
SQL Server Basics-datatable. Dispose ();. Clear (); = NULL;