Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers

How about:

foreach(SPWeb web in SPContext.Current.Site.AllWebs)
{
    using(web)
    {
        ...
    }
}
share|improve this answer
    
Yes that works. Double-checked with SPDisposeCheck and Reflector. –  Alex Angas Jan 9 '10 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/… –  Anders Rask Jan 9 '10 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. –  Jaap Vossers Jan 9 '10 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 –  Anders Rask Jan 9 '10 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 :) –  Jaap Vossers Jan 10 '10 at 15:15
show 1 more comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
so which one do you prefer? :) –  Jaap Vossers Jan 7 '10 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;-) –  Anders Rask Jan 8 '10 at 8:24
add comment

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.

share|improve this answer
    
That's a nice little gem! Thanks –  Jaap Vossers Jan 29 '12 at 11:56
add comment

Your Answer

 
discard

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.