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?