Thursday, 5 August 2010

The most elegant way of using disposable SharePoint objects


void CombiningCallsBestPractice()
{
using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
using (SPWeb web = siteCollection.OpenWeb())
{

//Perform operations on site.

}
// SPWeb object web.Dispose() automatically called; SPSite object
// siteCollection.Dispose() automatically called.
}

Source : http://msdn.microsoft.com/en-us/library/aa973248(office.12).aspx