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:
var obstests = new observablecollection<testviewmodel>(lst .select(t = > new testviewmodel(t));
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
Post a Comment