views:

62

answers:

3

Simple Question. Is it important to use the using statement when working with typed dataset table adapters? Most examples I have seen on the net never use the using statement.

using (Data_ItemTableAdapter adapter = new Data_ItemTableAdapter())
{

    DataItemTable = a.GetDataByDataItemID(DataItemID);
    // more code here
}

Thanks for your help!!

Edit: Seems like the general consensus is that it is probably a good idea to use "using" in this situation, because it certainly would not hurt, and it doesn't require much effort. Just not sure if it is worthwhile to go back through this old project and change all the code. Thanks for the advice!

+4  A: 

If a class implements the IDisposable interface then it is probably safer to wrap the object in a using statment or call the Dispose method manually.

In this case I don't believe that the data adapter is using any unmanged resources, so the dispose probably doesn't do anything substancial. However, it would be safer to wrap it because in the future it could.

Jerod Houghtelling
+1 For mentioning future safety
Jimmy Hoffa
+3  A: 

If someting implements IDisposable, I recommend wrapping it using() most of the time. Automatic call to Dispose() + Try/Finally for free? Hard to beat.

AllenG
adds a lot of readability to code when you do this, also, i've never even started to type the name of a disposed object under this style.
marr75
+2  A: 

If something implements dispose, that mean it probably has implemented a custom finalizer. Calling dispose should (if written correctly) suppress the CG from calling it. If the CG is required to call the finalizer of the class, the CG is going to have to do extra work in removing the object from memory. It's going to promote it to the next level of the CG, so the object is going to stay in memory a lot longer.

So even if you don't think it is that important to call it, it probably is a good idea anyway, as it will most likely allow for more efficient garbage collection. In truth if Dispose is there, it's probably there for a reason and should be used. Putting it in a using statement is really the easiest way to handle this.

Information on Finalizers and the GC in .NET

Kevin