1

I am developing an application in which for each method or event I used to write whole code in try...catch...finally block.Here is the structure of the code that I used to write.

//Some Declarations 
DataTable dt = new DataTable() 
try
{
     //Some code

}
Catch
{

}
Finally
{
  dt = null;
} 

This is how I used to write my code. My question is I am using datatable in try clause only. So is it necessary to dispose dt in the finally clause. Because I taught to do like this. But as far as my knowledge concern, if I declare dt in try clause, it will automatically disposed and set to null once the scope of try is over. So which one is better method?

Thanks in advance...

Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142

4 Answers4

3

dt = null; will not dispose the object. It just removes the reference. If the dt is in a method and its scope is ending in the position where the method ends, then there is no need to assign null to it.

Once you assign null to the variable, the object is eligible for garbage collection! I mean to say...the GC checks each object in the heap whether it is refered somewhere, if not then it marks it as garbage. Once all the objects are checked, the collection process begins. By assigning null to dt you are informing the GC that the object is garbage. So on the next collection it will be collected.

NaveenBhat
  • 3,248
  • 4
  • 35
  • 48
1

If the datatable is declared in the try or before your try block it will not be "disposed". Disposing an object is usually in relation to the IDisposable interface and using statements. Some objects do need to be disposed because they use resources in the background (Like a Sql Connection). I do not see any reason why this would need to be done to the datatable.

Additionally, I see no point in setting your dt to null in your finally block, it will not speed up garbage collection or close any other resources by doing that.

See this other Stack overflow question.... Should I Dispose() DataSet and DataTable?

Community
  • 1
  • 1
Stefan H
  • 6,635
  • 4
  • 24
  • 35
0

It's definitly a best practice to dispose all objects of IDisposable(if possible). If you're not concerned about exceptions you could use the using statement. Something like:

using(DataTable dt = new DataTable())
{
// code here
}
// datatable is disposed
JSC
  • 3,705
  • 3
  • 26
  • 25
  • @JSC..But what is the meaning to dispose those objects explicitly which ca be disposed by .net framework itself?? – Microsoft Developer Aug 08 '11 at 05:18
  • It's says to the garbage collector that that memory part is ready for garbage collection. If you've got a long running application something like a windows service and you're continuously creating objects who implements the IDisposable and you don't dispose them, then you app could leak memory and don't work as it suppost to do – JSC Aug 08 '11 at 05:22
  • The only reason DataTable has a Dispose method is because it inherits from MarshalByValueComponent and doing using(DataTable...) will call Dispose on all elements that implement the IDisposable interface on the resources used by the DataTable so unless DataTable has a reference to any unmanaged resources, the using statement accomplishes nothing. – Icarus Aug 08 '11 at 05:36
  • Disposal is not related to garbage collection. Disposal instructs an object to release non-memory resources like file handles or database connections. – recursive Aug 08 '11 at 05:37
  • @ JSC.. I can understand the function & importance of garbage collector but what I mean to say is why we as a developer should bother about disposing or removing reference for any object if .net framework itself is doing the same thing on behalf of developer. – Microsoft Developer Aug 08 '11 at 05:40
0

You can use the Using statement, if you like.

The statement :

using (ResourceType resource = expression) statement

is equivalent to

{  
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        ((IDisposable)resource).Dispose();
    }
}

So I guess you can code like :

using(DataTable dt = new DataTable)

midhunhk
  • 5,560
  • 7
  • 52
  • 83