Home
Never Dispose Objects from SPContext.Current
  v2.0 Posted at 29/08/2013 1:42 PM by System Account
​Disposing objects in SharePoint is important, but never do it with objects from SPContext.Current. SharePoint will manage disposing these objects itself.​

using (SPWeb web = SPContext.Current.Site.RootWeb)
{
 //do something here
}

Figure: Using statement is trying to dispose current site object - it will cause exception

Just simplely use "Current" object directly.

SPWeb web = SPContext.Current.Site.R​ootWeb​;
//do something here

Figure: Use Current objects directly - don't need to dispose them

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: