C# performance: construct new collection with existing items using constructor or loop? -


i've searched not found answer, maybe because question not easy describe. example in wpf, have model test, , list<test> lst, have construct observablecollection<testviewmodel> obstests. there maybe 2 ways:

  1. var obstests = new observablecollection<testviewmodel>(lst     .select(t = > new testviewmodel(t)); 
  2. var obstests = new observablecollection<testviewmodel>(); foreach(var test in lst) {     obstests.add(new testviewmodel(test)); } 

please tell me better in performance, , tell me best solution if asparallel available(such is observablecollection threadsafe? i'm using .net 4.5)

there no difference. ctor uses add method base class collection: reffer: click!

public observablecollection(list<t> list)     : base((list != null) ? new list<t>(list.count) : list) {     copyfrom(list); }  private void copyfrom(ienumerable<t> collection) {     ilist<t> items = items;     if (collection != null && items != null)     {         using (ienumerator<t> enumerator = collection.getenumerator())         {             while (enumerator.movenext())             {                 items.add(enumerator.current);             }         }     } } 

observablecollection basis on collection not thread-safe. thread-safe collection - use class concurrent namespace. more on msdn.

you can implement own-super-fast observable collection. here: click!


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -