Rule Definition
When using Items.Add in loops, you should use BeginUpdate / EndUpdate on the control to avoid unnecessarily refresh and thus improve the performance.
Remediation
Call to BeginUpdate() before the loop and EndUpdate() after the loop.
Alternatively, you can use AddRange method instead of Add.
Violation Code Sample
for(int i = 0; i < 10000; i++)
{
ListViewItem listItem = new ListViewItem("Item"+i.ToString() );
listView1.Items.Add(listItem);
}
Fixed Code Sample
listView1.BeginUpdate();
for(int i = 0; i < 10000; i++)
{
ListViewItem listItem = new ListViewItem("Item"+i.ToString() );
listView1.Items.Add(listItem);
}
listView1.EndUpdate();
Reference
MSDN
Related Technologies
.Net
Technical Criterion
Efficiency - Expensive Calls in Loops
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.