List<T> vs ObservableCollection<T>

Hello,
I have a quick question on using ItemsSources for ListViews; What are the benefits of using a List over an ObservableCollection?
At a quick glance, from what I've seen, should you use ObservableCollection every time?
Thank you for replying!

Answers

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    The answer above explains the technical differences. However, with regards to ListView (and I presume this refers to Xamarin.Forms) there is another important difference: if you use a List<T> and add or remove items at runtime. your ListView will not reflect those changes. If you use an ObservableCollection<T>, changes will be reflected.
    In other words: if you know that your data is static, go for a List<T>.

  • PierceBogganPierceBoggan USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    It's also worth noting that if you plan on adding multiple items to your ObservableCollection at once, you should check out something like ObservableRangeCollection:
    https://github.com/jamesmontemagno/mvvm-helpers/blob/master/MvvmHelpers/ObservableRangeCollection.cs

    It works just like an ObservableCollection, but adds an AddRange method. This is awesome for two reasons:

    1. You don't have to loop through the List<T> to add them to an ObservableCollection, which is pretty common to have to do.
    2. You get massive performance gains. When the CollectionChanged event is fired, the UI will update. If you are adding 10, 25, 50 items to a ListView, this means that your UI is reloading that number of times. It's best to use the ObservableRangeCollection for circumstances like this. :)
  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    @pierce.boggan I'm really surprised that's not baked into .Net yet - it's something that everyone doing MVVM has to either implement to get from a framework. Same as a RelayCommand - should be in .net already!

  • amirvenusamirvenus USMember ✭✭✭

    @PierceBoggan said:
    It's also worth noting that if you plan on adding multiple items to your ObservableCollection at once, you should check out something like ObservableRangeCollection:
    https://github.com/jamesmontemagno/mvvm-helpers/blob/master/MvvmHelpers/ObservableRangeCollection.cs

    It works just like an ObservableCollection, but adds an AddRange method. This is awesome for two reasons:

    1. You don't have to loop through the List<T> to add them to an ObservableCollection, which is pretty common to have to do.
    2. You get massive performance gains. When the CollectionChanged event is fired, the UI will update. If you are adding 10, 25, 50 items to a ListView, this means that your UI is reloading that number of times. It's best to use the ObservableRangeCollection for circumstances like this. :)

    This should be the default ObservableCollection in .NET

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited May 13

    2016 and still waiting

    @JimBennett said:
    @pierce.boggan I'm really surprised that's not baked into .Net yet - it's something that everyone doing MVVM has to either implement to get from a framework. Same as a RelayCommand - should be in .net already!

    @amirvenus said:
    This should be the default ObservableCollection in .NET

    Yep. It was in the Xamarin Evolution forum area... but that's dead now. This probably got "slipped through the cracks" at that point.

    Maybe someone should post it in whatever is this week's social media, bug report, feature request area that Xamarin and Microsoft are listening to. Then the rest of us can pile on with +1 votes for it.

    I mean, we don't need another collection type. Just give ObservableCollection the concept of Range support.

    cc: @JamesMontemagno @DavidOrtinau @JGoldberger

Sign In or Register to comment.