thebeebs | Return inside a using
thebeebs
Zeroing the desk - Ignore the design
 
 

Return inside a using

by thebeebs 11. November 2008 02:28

Someone asked me the other day why I returned inside a using like this:

using (SiteEntities context = new SiteEntities(Environment.GetConnectionString("SiteEntities")))           {                HelpItems originalItem = (from h in context.HelpItems                                          where h.Id== Item.Id                                          select h).FirstOrDefault();                 originalItem.ModifiedBy = item.ModifiedBy;                 return context.SaveChanges();            }

Their comment was, well your not closing the using so the Context won't dispose.

This is wrong... Even though we return, the context does dispose, this is the way the using works and one of the reasons it's so useful.

Tags:

Comments are closed