<p>Was considering the <code>System.Collections.ObjectModel ObservableCollection&lt;T&gt;</code> class. This one is strange because  </p>

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

<p>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?</p>

<p><em>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.</em></p>

<hr>

<p>Related Q:<br>
<a href="https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each/670579#670579">https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each</a></p>

[{'answer_id': 57029, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': '<p>Inherit from List&lt;T> and override the Add() and AddRange() methods to raise an event?</p>\n'}, {'answer_id': 57036, 'author': 'David Mohundro', 'author_id': 4570, 'author_profile': 'https://Stackoverflow.com/users/4570', 'pm_score': 2, 'selected': False, 'text': "<p>If you're wanting to inherit from a collection of some sort, you're probably better off inheriting from System.Collections.ObjectModel.Collection because it provides virtual methods for override. You'll have to shadow methods off of List if you go that route.</p>\n\n<p>I'm not aware of any built-in collections that provide this functionality, though I'd welcome being corrected :)</p>\n"}, {'answer_id': 57069, 'author': 'fryguybob', 'author_id': 4592, 'author_profile': 'https://Stackoverflow.com/users/4592', 'pm_score': 4, 'selected': False, 'text': "<p>It seems that the <code>INotifyCollectionChanged</code> interface allows for updating when multiple items were added, so I'm not sure why <code>ObservableCollection&lt;T&gt;</code> doesn't have an <code>AddRange</code>.  You could make an extension method for <code>AddRange</code>, but that would cause an event for every item that is added.  If that isn't acceptable you should be able to inherit from <code>ObservableCollection&lt;T&gt;</code> as follows:</p>\n\n<pre><code>public class MyObservableCollection&lt;T&gt; : ObservableCollection&lt;T&gt;\n{\n    // matching constructors ...\n\n    bool isInAddRange = false;\n\n    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)\n    {\n        // intercept this when it gets called inside the AddRange method.\n        if (!isInAddRange) \n            base.OnCollectionChanged(e);\n    }\n\n\n    public void AddRange(IEnumerable&lt;T&gt; items)\n    {\n         isInAddRange = true;\n         foreach (T item in items)\n            Add(item);\n         isInAddRange = false;\n\n         var e = new NotifyCollectionChangedEventArgs(\n             NotifyCollectionChangedAction.Add,\n             items.ToList());\n         base.OnCollectionChanged(e);\n    }\n}\n</code></pre>\n"}, {'answer_id': 58255, 'author': 'Ryan Lundy', 'author_id': 5486, 'author_profile': 'https://Stackoverflow.com/users/5486', 'pm_score': 2, 'selected': False, 'text': '<p>Not only is <code>System.Collections.ObjectModel.Collection&lt;T&gt;</code> a good bet, but in the help docs there\'s <a href="http://msdn.microsoft.com/en-us/library/ms132397.aspx" rel="nofollow noreferrer">an example</a> of how to override its various protected methods in order to get notification.  (Scroll down to Example 2.)</p>\n'}, {'answer_id': 61564, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 3, 'selected': False, 'text': "<p>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 :)\nTested Snippet follows...</p>\n\n<pre><code>using System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Collections.Specialized;\n\nnamespace MyNamespace\n{\n    public class ObservableCollectionWithBatchUpdates&lt;T&gt; : ObservableCollection&lt;T&gt;\n    {\n        public void AddRange(ICollection&lt;T&gt; obNewItems)\n        {\n            IList&lt;T&gt; obAddedItems = new List&lt;T&gt;();\n            foreach (T obItem in obNewItems)\n            {\n                Items.Add(obItem);\n                obAddedItems.Add(obItem);\n            }\n            NotifyCollectionChangedEventArgs obEvtArgs = new NotifyCollectionChangedEventArgs(\n               NotifyCollectionChangedAction.Add, \n               obAddedItems as System.Collections.IList);\n            base.OnCollectionChanged(obEvtArgs);\n        }\n\n    }\n}\n</code></pre>\n"}, {'answer_id': 851197, 'author': 'Sam Saffron', 'author_id': 17174, 'author_profile': 'https://Stackoverflow.com/users/17174', 'pm_score': 2, 'selected': False, 'text': '<p>If you use any of the above implementations that send an add range command and bind the observablecolletion to a listview you will get this nasty error. </p>\n\n<pre>\nNotSupportedException\n   at System.Windows.Data.ListCollectionView.ValidateCollectionChangedEventArgs(NotifyCollectionChangedEventArgs e)\n   at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)\n   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)\n   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)\n</pre> \n\n<p>The implementation I have gone with uses the Reset event that is more evenly implemented around the WPF framework: </p>\n\n<pre><code>    public void AddRange(IEnumerable&lt;T&gt; collection)\n    {\n        foreach (var i in collection) Items.Add(i);\n        OnPropertyChanged("Count");\n        OnPropertyChanged("Item[]");\n        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));\n    }\n</code></pre>\n'}, {'answer_id': 1123307, 'author': 'Shimmy Weitzhandler', 'author_id': 75500, 'author_profile': 'https://Stackoverflow.com/users/75500', 'pm_score': 0, 'selected': False, 'text': '<p>Take a look at <a href="https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each/670579#670579">Observable collection with AddRange, RemoveRange and Replace range methods</a> in both C# and VB.</p>\n\n<p>In VB: INotifyCollectionChanging implementation.</p>\n'}, {'answer_id': 1790438, 'author': 'Boris', 'author_id': 217845, 'author_profile': 'https://Stackoverflow.com/users/217845', 'pm_score': 0, 'selected': False, 'text': '<p>For fast adding you could use:</p>\n\n<pre><code>((List&lt;Person&gt;)this.Items).AddRange(NewItems);\n</code></pre>\n'}, {'answer_id': 3829525, 'author': 'Akash Kava', 'author_id': 85597, 'author_profile': 'https://Stackoverflow.com/users/85597', 'pm_score': 2, 'selected': False, 'text': '<p>I have seen this kind of question many times, and I wonder why even Microsoft is promoting ObservableCollection everywhere where else there is a better collection already available thats.. </p>\n\n<blockquote>\n  <p><code>BindingList&lt;T&gt;</code></p>\n</blockquote>\n\n<p>Which allows you to turn off notifications and do bulk operations and then turn on the notifications. </p>\n'}, {'answer_id': 8837627, 'author': 'Mo0gles', 'author_id': 283512, 'author_profile': 'https://Stackoverflow.com/users/283512', 'pm_score': 2, 'selected': False, 'text': '<p>Another solution that is similar to the CollectionView pattern:</p>\n\n<pre><code>public class DeferableObservableCollection&lt;T&gt; : ObservableCollection&lt;T&gt;\n{\n    private int deferLevel;\n\n    private class DeferHelper&lt;T&gt; : IDisposable\n    {\n        private DeferableObservableCollection&lt;T&gt; owningCollection;\n        public DeferHelper(DeferableObservableCollection&lt;T&gt; owningCollection)\n        {\n            this.owningCollection = owningCollection;\n        }\n\n        public void Dispose()\n        {\n            owningCollection.EndDefer();\n        }\n    }\n\n    private void EndDefer()\n    {\n        if (--deferLevel &lt;= 0)\n        {\n            deferLevel = 0;\n            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));\n        }\n    }\n\n    public IDisposable DeferNotifications()\n    {\n        deferLevel++;\n        return new DeferHelper&lt;T&gt;(this);\n    }\n\n    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)\n    {\n        if (deferLevel == 0) // Not in a defer just send events as normally\n        {\n            base.OnCollectionChanged(e);\n        } // Else notify on EndDefer\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/57020', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1695/']