[EMAIL PROTECTED] wrote:
> > In my code i have a method with just one line - as follows:
> >
> > return ProductCatalog.GetProduct(productId).GetCategoryProperties().Tables
> > [0].DataRow[0]["Title"];
> >
> > The result of the GetProduct(productId) bit is an object that has a Dispose
> > () method. Do i need to worry about calling this Dispose method? That would
> > mean breaking down the above into multiple lines as follows:
> >
> > string title = String.Empty;
> > Product p = ProductCatalog.GetProduct(productId);
> > title = p.GetCategoryProperties().Tables[0].DataRow[0]["Title"];
> > p.Dispose();
> > p = null;
> > return title;
> >
> > Is one of these better than the other from a resource management point of
> > view? Any help appreciated.
> >
>
> If a class implements IDisposable then to be on the safe side, you'll
> want to call Dispose as soon as you are done with an instance of the
> class. You can avoid breaking the code into 4-5 lines with code like
> this:
>
> using (Product p = ProductCatalog.GetProduct(productId))
>   return p.GetCategoryProperties().Tables[0].DataRow[0]["Title"];
>
> Regards,
> Sami

My understanding of the "using" statement (if used properly) is that it will result in 
pretty much the same outcome as the second option i suggested above but just be 
syntactically different. What i want to know is if the first line of code above 
actually needs disposing. Maybe another question to ask to get to the answer i want 
is: if a reference to an object never exists does it need disposing of? My gut feeling 
on the above is it should be broken down but it would be good to get an explaination.

Nick

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to