Can the below foreach
loop be improved? Since I use the same method twice, maybe use continue
or break
?
int? index = null;
int count = 0;
foreach (Break b in breaks)
{
if (index.HasValue)
{
if (index == count)
b.Remove();
}
else
{
b.Remove();
}
count++;
}
So I turned it into this:
foreach (Break b in breaks)
{
if ((index.HasValue && index == count) || !index.HasValue)
{
b.Remove();
}
count++;
}
Which is the same. Any other optimizations possible?
index
is alwaysnull
, so why not simply remove it?int count = 0; foreach(Break b in breaks { b.Remove(); count++ }
? What value does havingindex
serve? \$\endgroup\$a ==> b
equivalent to(a && b) || !a
, or perhapslet x = a in (x && b) || !x
in the presence of side effects. Moreover, where’s mylet
-in
expression? \$\endgroup\$