Hi,
Suppose we want to loop through all the items in a dropdown list and no item is added or removed while we are looping. The code for it is as follows:
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Text == text)
{
found = true;
break;
}
}
If it is changed to this:
for (int i = 0, c = ddl.Items.Count; i < c; i++)
{
if (ddl.Items[i].Text == text)
{
found = true;
break;
}
}
Is there any performance gain? Does the compiler do something clever not to read the Count
property every iteration?