.NET ObservableDictionary

The Microsoft ParallelExtensionsExtras provides this class which is not only observable but is also concurrent: Now available via Nuget: https://www.nuget.org/packages/MSFT.ParallelExtensionsExtras/ The source code was recently updated for .NET Standard 2.1 on 05/11/2020 and the source code is available at GitHub: https://github.com/dotnet/samples/tree/master/csharp/parallel/ParallelExtensionsExtras Note that some of the features are now a part of newer .NET frameworks. … Read more

Fast performing and thread safe observable collection

ObservableCollection can be fast, if it wants to. 🙂 The code below is a very good example of a thread safe, faster observable collection and you can extend it further to your wish. using System.Collections.Specialized; public class FastObservableCollection<T> : ObservableCollection<T> { private readonly object locker = new object(); /// <summary> /// This private variable holds … Read more

Sort ObservableCollection through C#

Introduction Basically, if there is a need to display a sorted collection, please consider using the CollectionViewSource class: assign (“bind”) its Source property to the source collection — an instance of the ObservableCollection<T> class. The idea is that CollectionViewSource class provides an instance of the CollectionView class. This is kind of “projection” of the original … Read more

Optimal LINQ query to get a random sub collection – Shuffle

Further to mquander’s answer and Dan Blanchard’s comment, here’s a LINQ-friendly extension method that performs a Fisher-Yates-Durstenfeld shuffle: // take n random items from yourCollection var randomItems = yourCollection.Shuffle().Take(n); // … public static class EnumerableExtensions { public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) { return source.Shuffle(new Random()); } public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng) … Read more

Difference between ObservableCollection and BindingList

An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward: ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList<T> implements IBindingList. IBindingList provides notification on collection changes, … Read more

ObservableCollection Doesn’t support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

Please refer to the updated and optimized C# 7 version. I didn’t want to remove the VB.NET version so I just posted it in a separate answer. Go to updated version Seems it’s not supported, I implemented by myself, FYI, hope it to be helpful: I updated the VB version and from now on it … Read more

How do I update an ObservableCollection via a worker thread?

New option for .NET 4.5 Starting from .NET 4.5 there is a built-in mechanism to automatically synchronize access to the collection and dispatch CollectionChanged events to the UI thread. To enable this feature you need to call BindingOperations.EnableCollectionSynchronization from within your UI thread. EnableCollectionSynchronization does two things: Remembers the thread from which it is called … Read more