Home » ASP.NET | Code

Return inside a using

10. November 2008 by thebeebs 0 Comments

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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Add comment

(Will show your Gravatar icon)  

  Country flag

  • Comment
  • Preview