Using C# it's common practice (I think) to omit indentation and brackets with nested using statements like this:
using (var fileStream = new FileStream("filename"))
using (var streamReader = new StreamReader(fileStream))
using (var myCustomReader = new MyCustomReader(streamReader ))
{
///...
}
I've been refactorying some code recently that's suffering from messy nesting of if and for loops, I've been using continues/returns to reduce this nesting but I also experimented with using this style with the for loops, so:
foreach (var cart in carts)
{
foreach (var item in cart.items)
{
foreach (var charge in item.charges)
{
///...
}
}
}
Becomes:
foreach (var cart in carts)
foreach (var item in cart.items)
foreach (var charge in item.charges)
{
///...
}
Which seems much easier to read to me.
Is there a good reason not to use such styling? Is it particularly uncommon/might cause problems with other developers after me?
foreach
's.