Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 639053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:47:58+00:00 2026-05-13T20:47:58+00:00

I read the msdn article about disposing objects in http://msdn.microsoft.com/en-us/library/ee557362(office.14).aspx now I’m really confused

  • 0

I read the msdn article about disposing objects in http://msdn.microsoft.com/en-us/library/ee557362(office.14).aspx

now I’m really confused about this.
consider this example

SPList List = SPContext.Current.Web.Lists["DemoList"];
SPListItem Item = List.GetItemById(ItemID);

is it ok to use this or better to use:

using (SPWeb web = SPContext.Current.Web)
            {
                SPList List= web.Lists["DemoList"];
                SPListItem Item = List.GetItemById(ItemID);

             }

or it makes no difference

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:47:59+00:00Added an answer on May 13, 2026 at 8:47 pm

    You don’t need to dispose of the SPWeb in this case as you didn’t create it. You only need to dispose of an SPWeb object (and SPSite object) if you are responsible for instantiating the object.

    So in the following instance you would need to call dispose (or have dispose automatically aclled using the “using” statement) as you were responsible for new-ing up the SPSite..

    void CombiningCallsBestPractice()
    {
        using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
        {
            using (SPWeb web = siteCollection.OpenWeb())
            {
                // Perform operations on site.
            } // SPWeb object web.Dispose() automatically called.
        }  // SPSite object siteCollection.Dispose() automatically called.
    }
    

    The “using” statement is equivalant to calling web.Dispose() at the end of the block, but is more readable and the disposal is less likely to be forgotten.

    If you are worried about whether you have any undisposed objects in your SharePoint code I strongly recommend using SPDisposeCheck. This tool will analyse your assembly and point out all the places where you could have an undisposed object. It’s great! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure that I understand Isolated storage. I read the article http://msdn.microsoft.com/en-us/library/3ak841sy%28VS.80%29.aspx
I know all about this exception, read the msdn article here http://support.microsoft.com/kb/312629/EN-US/ but I
I've read the MSDN article about the layouts pass, that states: When a node
I've already read the MSDN article about it. It seems internally it is the
I have read the MSDN article on MVVM and I am not really convinced.
I am using IMoniker::BindToObject function, and I have read the article on MSDN. The
According to this MSDN article about medium trust , under medium-trust: FileIOPermission is restricted.
Once I read an MSDN article that encouraged the following programming paradigm (its not
I just read through a 2002 article on MSDN called Calling a .NET Component
In this MSDN article on file sharing mode with std::ofstream , Microsoft writes: To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.