1

I've searched but not found answer, maybe because this question is not easy to describe. For example in WPF, I have a model Test, and a List<Test> lst, then have to construct a 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 which is better in performance, and tell me the best solution if AsParallel is available(such as is ObservableCollection threadsafe? I'm using .net 4.5)

svick
  • 236,525
  • 50
  • 385
  • 514
Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • 4
    Hint: if you have a performance issue; then start profiling. If you don't have a perfomance issue; then focus on writing easy-to-read code. – GhostCat Jun 28 '16 at 09:36
  • I have performance issue, but there are too many combinations, adding parallel for example, and I think it is a valuable common question for all developers. – Lei Yang Jun 28 '16 at 09:39
  • First and second approaches are identical in performance. When you pass in collection into ObservableCollection it performs foreach loop inside in ctor. – Karolis Kajenas Jun 28 '16 at 09:43
  • @Carl from nopeflow's answer, I know now too... thx – Lei Yang Jun 28 '16 at 09:47
  • 1
    Read https://ericlippert.com/2012/12/17/performance-rant/ – Kris Vandermotten Jun 28 '16 at 10:04
  • You might want to look at System.Threading.Tasks.Parallel.ForEach (https://msdn.microsoft.com/en-gb/library/system.threading.tasks.parallel(v=vs.110).aspx) or AsParallel which in some cases can improve performance. – apc Jun 29 '16 at 14:43
  • @apc I already know that. but there are too many choices for Task, Parellel, even regarding Parellel, there are ForAll, Foreach... – Lei Yang Jun 30 '16 at 01:01

2 Answers2

2

There is no difference. Ctor uses Add method from 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 so it is not thread-safe. For thread-safe collection - use some class from Concurrent namespace. More on MSDN.

You can also implement own-super-fast observable collection. Like here: Click!

Community
  • 1
  • 1
pwas
  • 3,225
  • 18
  • 40
-1

In case if you have standard implementation of some action, and can do the same using your code, it is better to choose the standard implementation. Also it looks more tiny :)

Waldemar
  • 5,363
  • 3
  • 17
  • 28