Avoiding N*2 iteration

Download c# eBook

Example

This is placed in a Windows Forms event handler

var nameList = new BindingList<string>();
ComboBox1.DataSource = nameList;
for(long i = 0; i < 10000; i++ ) {
    nameList.AddRange(new [] {"Alice", "Bob", "Carol" });
} 

This takes a long time to execute, to fix, do the below:

var nameList = new BindingList<string>();
ComboBox1.DataSource = nameList;
nameList.RaiseListChangedEvents = false;
for(long i = 0; i < 10000; i++ ) {
    nameList.AddRange(new [] {"Alice", "Bob", "Carol" });
} 
nameList.RaiseListChangedEvents = true;
nameList.ResetBindings();

Stats

Contributors: 2
2016-09-30
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook