Advertisement
DigitalMag

ObservableHashSet

Jun 8th, 2020
1,469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Specialized;
  6.  
  7. using ChangedEventArgs = System.Collections.Specialized.NotifyCollectionChangedEventArgs;
  8. using Actions = System.Collections.Specialized.NotifyCollectionChangedAction;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.        
  15.        
  16.         static void Main(string[] args)
  17.         {
  18.             var rs = new ObservableHashSet<int>();
  19.             for (int i = 0; i < 15; i++)
  20.             {
  21.                 rs.Add(i);
  22.             }
  23.             rs.CollectionChanged += (s, e) =>
  24.             {
  25.                 var r = e.OldItems;
  26.                 switch (e.Action)
  27.                 {
  28.                     case Actions.Add: Console.WriteLine(e.NewItems[0]); break;
  29.                     case Actions.Remove: Console.WriteLine(e.OldItems[0]); break;
  30.                     case Actions.Reset: Console.WriteLine("rm"); break;
  31.                 }                
  32.             };
  33.             rs.Add(18);
  34.             rs.Remove(18);
  35.             rs.Clear();
  36.  
  37.             Console.WriteLine(rs.Count);
  38.             Console.Read();
  39.         }
  40.     }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.     public class ObservableHashSet<T> : HashSet<T>, INotifyCollectionChanged
  47.     {
  48.         public event NotifyCollectionChangedEventHandler CollectionChanged;
  49.  
  50.         public ObservableHashSet() { }
  51.         public ObservableHashSet(IEnumerable<T> list)
  52.             : base(list){}
  53.  
  54.         public new bool Add(T value)
  55.         {
  56.             if (base.Add(value))
  57.             {
  58.                 if (CollectionChanged != null) CollectionChanged.Invoke(this, new ChangedEventArgs(Actions.Add, value));
  59.                 return true;
  60.             }
  61.  
  62.             return false;
  63.         }
  64.  
  65.         public new bool Remove(T value)
  66.         {                        
  67.             if( base.Remove(value))
  68.             {
  69.                 if (CollectionChanged != null) CollectionChanged.Invoke(this, new ChangedEventArgs(Actions.Remove, value));
  70.                 return true;
  71.             }
  72.             return false;                
  73.         }
  74.  
  75.         public new int Clear()
  76.         {
  77.             int cnt = base.Count;
  78.             if (cnt > 0)
  79.             {
  80.                 base.Clear();
  81.                 if (CollectionChanged != null) CollectionChanged.Invoke(this, new ChangedEventArgs(Actions.Reset, null));
  82.                 return -cnt;
  83.             }
  84.             return 0;
  85.         }
  86.  
  87.         public void OnCollectionChanged(ChangedEventArgs e)
  88.         {
  89.             if (CollectionChanged != null) CollectionChanged.Invoke(this, e);
  90.         }
  91.  
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement