SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If an IDisposable SharePoint object is passed into a function, does an explicit Dispose() call need to be made on it?


public void TheCallingFunction()
    SPSite site = SPContext.Current.Site;
    using (SPWeb web = site.OpenWeb("WebName"))

public void DoSomethingRad(SPWeb web)
    // Do something that's borderline awesome

    web.Dispose(); // Yes or no?

Roger Lamb's SharePoint 2007 and WSS 3.0 Dispose Patterns by Example doesn't cover this directly (really more of a C# question than an SP question I suppose).

share|improve this question
up vote 2 down vote accepted

If an IDisposable SharePoint object is passed into a function, does an explicit Dispose() call need to made on it?

No. Generally, whoever created the object is also responsible for disposing of it. In your example, TheCallingFunction might have had some other use for the web after DoSomethingRad returns. See also this StackOverflow question.

Most of the time calling Dispose() twice in a row won't break your program, but it might lead to unexpected results when you're trying to use an object that's been disposed.

share|improve this answer
That was exactly what I thought, I just wanted to verify if passing the object created another instance or not. Just like C++, if you create it, you delete it. Trying to clean up someone else's code... – RJ Cuthbertson Dec 5 '12 at 18:53
You might want to DoSomethingRad(SPContext.Current.Web); or DoSomethingRad(web); SPList awesome = web.GetList("RadList");, in which case it would be a bad thing to dispose of the object in DoSomethingRad() – lgaud Dec 5 '12 at 18:59

No. If you do, you might actually get an error, because the using statement under "TheCallingFunction" would call dispose on "web" after "DoSomethingRad" already called "Dispose()".

  • As a rule of thumb, you have to look at your public method as a black box that just does what you expect it to do. To follow the Single-Responsibility Principle just do "DoSomethingRad" on your case, nothing else.
share|improve this answer

AFAIK, using will dispose the object... So you don't need to explicitly dispose the object when passing as parameter to some other function!

As you can also see if you try to pass the object as ref to the function, it will give compile-time error that a variable in using can't be passed as reference!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.