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).

3 Answers 3


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.

  • 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... Dec 5, 2012 at 18:53
  • 1
    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, 2012 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.

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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