title: C# Dictionary change notification (INotifyCollectionChanged)
body: <p>I have a loop running retrieving live share prices. What I want to do is check if any of the prices retrieved are different to the price I already have stored in a dictionary and notify me with details of all those that have changed. Was looking along the lines of Dictionary utilising INotifyCollectionChanged.</p>
  
  <p>e.g.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Linq;
  
  namespace BasicExamples
  {
      class CollectionNotify:INotifyCollectionChanged
      {
          public Dictionary&lt;string, string&gt; NotifyDictionary{ get; set; }     
  
          public CollectionNotify()
          {
              NotifyDictionary = new Dictionary&lt;string, string&gt;();
          }
  
          public event NotifyCollectionChangedEventHandler CollectionChanged;
  
          protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
          {
              if (CollectionChanged != null)
              {
                  CollectionChanged(this, e);
              }
          }
  
          public void Add(object k, object v)
          {
              NotifyDictionary.Add(k.ToString(),v.ToString());
              OnCollectionChanged(new     NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,0));
          }
  
          public void Update(object k, object v)
          {
              bool isUpdated = false;
              IList&lt;string&gt; changedItems = new List&lt;string&gt;();
              int index;
              if (NotifyDictionary.ContainsKey(k.ToString()))
              {
                  NotifyDictionary[k.ToString()] = v.ToString();
                  changedItems.Add(k+":"+v);
                  isUpdated = true;
              }
              else
              {
                  Add(k, v);
              }
              if (isUpdated)
              {
                  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,changedItems,changedItems));
              }                
          }
      }
  }
  </code></pre>
  
  <p>with the main program calling Add/Update as a test. However when looping through the NotifyCollectionChangedEventArgs I have to nest the loop and cast to IEnumerable - this seems like a really long winded way round.</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace BasicExamples
  {
      class Program
      {
          //delegate 
          static void Main(string[] args)
          {
              //notify collection
              CollectionNotify notify = new CollectionNotify();
              notify.CollectionChanged += CollectionHasChanged;
  
              notify.Add("Test1", "Test2");
              notify.Add("Test2","Test2");
              notify.Add("Test3", "Test3");
              notify.Update("Test2", "Test1");
              notify.Update("Test2", "Test3");
              notify.Update("Test3", "Test1");
              notify.Update("Test1", "Test3");
  
              #region Lamba
  
              //lamba
              List&lt;int&gt; myList = new List&lt;int&gt;() {1,1,2,3,4,5,6};
  
              List&lt;int&gt; newList = myList.FindAll(s =&gt;
              {
                  if (s == 1)
                  {
                      return true;
                  }
                  return false;
              });
  
              foreach (int b in newList)
              {
                  Console.WriteLine(b.ToString());
              }
  
              #endregion
          }
  
          public static void CollectionHasChanged(object sender, EventArgs e)
          {
              NotifyCollectionChangedEventArgs args = (NotifyCollectionChangedEventArgs) e;
              if (args.Action == NotifyCollectionChangedAction.Replace)
              {
                  foreach (var item in args.NewItems)
                  {
                      foreach (var nextItem in (IEnumerable)item)
                      {
                          Console.WriteLine(nextItem);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I suppose my question is two fold:-
  A: Is this the best way to use a notifiable dictionary?
  B: Should i use the nested loop as above to retrieve the updated values?</p>
  
  <p>Thanks in advance</p>