1

I was just wondering which of the two methods below is more popular, when it comes to iterating over a collection of SPWeb objects.

for (int i = 0; i < SPContext.Current.Site.AllWebs.Count; i++)
{
    using (SPWeb web = SPContext.Current.Site.AllWebs[i])
    {

    }
}

--vs--

foreach(SPWeb web in SPContext.Current.Site.AllWebs)
{
    try
    {

    }
    catch
    {

    }
    finally
    {
        web.Dispose();
    }
}

I personally prefer using foreach over for in general, but I also prefer using the using statement over try/catch/finally/dispose for SPWeb disposal.

I find myself using both (depending on my mood I guess???) but would like to get rid of that feeling of indecision.

Which one do you prefer and why? Did I miss any alternatives?

5 Answers 5

2

How about:

foreach(SPWeb web in SPContext.Current.Site.AllWebs)
{
    using(web)
    {
        ...
    }
}
6
  • Yes that works. Double-checked with SPDisposeCheck and Reflector.
    – Alex Angas
    Jan 9, 2010 at 0:25
  • 2
    hmmmmm i guess i looked more at the pattern than at the code itself, because as i see it none of the above patterns should be disposed (they all come from SPContext). SPDisposeCheck wouldnt catch this since it doesnt check for false negatives. Also check Roger Lambs "Do not dispose" guidance here blogs.msdn.com/rogerla/archive/2009/11/30/… Jan 9, 2010 at 15:41
  • the original question was related to the code itself rather than disposing when needed. I like MBSurf's suggestion to be honest. @Anders, I do think that SPWebs retrieved from the AllWebs collection should be disposed, even though the SPSite was retrieved from the SPContext. I guess we should create a new question for this if we want to discuss it any further, so this thread stays on-topic. Jan 9, 2010 at 17:53
  • 1
    well but its relevant when the code sample is disposing when it shouldnt, and Alex mentioned SPDisposeCheck so i felt it was relevant Jan 9, 2010 at 19:50
  • @Anders, sorry man, I wasn't saying your comment was irrelevant. I meant to say that any further discussions on this would ideally be put under a separate question :) Jan 10, 2010 at 15:15
1

I tend to use the foreach. It seems more natural than checking it against the count property. I have never had a problem adding the try/catch/finally blocks, if anything I overuse them.

1

you could also check out the SharePoint Extensions Library which has some nifty extensions methods for the SharePoint API. Especially check the extension for SPWebCollection and the AsSafeEnumerable() extension method. Makes your code looks really clean!

/WW

1

in IL the parser converts using to try/finally, so performance wise there is no difference.

Oh, and you ofcourse shouldnt put a catch in there unless you are going to do something with it or rethrow, or you will swallow all exceptions ;-)

2
  • so which one do you prefer? :) Jan 7, 2010 at 23:34
  • since performance are basicly identical, i would always opt for readability. Mantra: you write code once but read it (or others do) > once so I always (if no performance implications) try to code for readability. foreach wins hands down (as long as you remove that ugly catch;-) Jan 8, 2010 at 8:24
1

Another approach is to use the WebsInfo member of SPWebCollection (AllWebs). http://www.ilovesharepoint.com/2011/07/perfect-loop-looping-through-all-webs.html. If you need a limited set of properties, you don't have to worry about disposing it because it is not IDisposable and it is performant.

But if you are using AllWebs, you need to dispose each web only when you access it.

0

Your Answer

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

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