Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which .Net collection for adding multiple objects at once and getting notified?

Was considering the System.Collections.ObjectModel ObservableCollection<T> class. This one is strange because

  • it has an Add Method which takes one item only. No AddRange or equivalent.
  • the Notification event arguments has a NewItems property, which is a IList (of objects.. not T)

My need here is to add a batch of objects to a collection and the listener also gets the batch as part of the notification. Am I missing something with ObservableCollection ? Is there another class that meets my spec?

Update: Don't want to roll my own as far as feasible. I'd have to build in add/remove/change etc.. a whole lot of stuff.


Related Q:
https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each

like image 279
Gishu Avatar asked Sep 11 '08 16:09

Gishu


People also ask

Which collection is faster in c#?

ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey,TValue> generic class provides faster lookup than the SortedDictionary<TKey,TValue> generic class.

What is the use of ObservableCollection in c#?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

Can we create multiple objects using one class in C#?

Note that a class is a type. You can think of it as template used to create objects (also called instances of this class). So, you can have many different objects of the same class.


2 Answers

It seems that the INotifyCollectionChanged interface allows for updating when multiple items were added, so I'm not sure why ObservableCollection<T> doesn't have an AddRange. You could make an extension method for AddRange, but that would cause an event for every item that is added. If that isn't acceptable you should be able to inherit from ObservableCollection<T> as follows:

public class MyObservableCollection<T> : ObservableCollection<T> {     // matching constructors ...      bool isInAddRange = false;      protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)     {         // intercept this when it gets called inside the AddRange method.         if (!isInAddRange)              base.OnCollectionChanged(e);     }       public void AddRange(IEnumerable<T> items)     {          isInAddRange = true;          foreach (T item in items)             Add(item);          isInAddRange = false;           var e = new NotifyCollectionChangedEventArgs(              NotifyCollectionChangedAction.Add,              items.ToList());          base.OnCollectionChanged(e);     } } 
like image 129
fryguybob Avatar answered Sep 19 '22 05:09

fryguybob


Well the idea is same as that of fryguybob - kinda weird that ObservableCollection is kinda half-done. The event args for this thing do not even use Generics.. making me use an IList (that's so.. yesterday :) Tested Snippet follows...

using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized;  namespace MyNamespace {     public class ObservableCollectionWithBatchUpdates<T> : ObservableCollection<T>     {         public void AddRange(ICollection<T> obNewItems)         {             IList<T> obAddedItems = new List<T>();             foreach (T obItem in obNewItems)             {                 Items.Add(obItem);                 obAddedItems.Add(obItem);             }             NotifyCollectionChangedEventArgs obEvtArgs = new NotifyCollectionChangedEventArgs(                NotifyCollectionChangedAction.Add,                 obAddedItems as System.Collections.IList);             base.OnCollectionChanged(obEvtArgs);         }      } } 
like image 37
Gishu Avatar answered Sep 19 '22 05:09

Gishu