Avoiding N*2 iteration C# with Example



Avoiding N*2 iteration C# with Example

This is placed in a Windows Forms event handler 
var nameList = new BindingList(); 
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(); 
ComboBox1.DataSource = nameList; 
nameList.RaiseListChangedEvents = false; 
for(long i = 0; i < 10000; i++ ) { 
nameList.AddRange(new [] {"Alice", "Bob", "Carol" }); 
} 
nameList.RaiseListChangedEvents = true; 
nameList.ResetBindings(); 
 

0 Comment's

Comment Form

Submit Comment