Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 2.52 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C# HashSet2 to work exactly like the standard C# HashSet, not compiling
  2. public class HashSet2<T> : ICollection<T>
  3. {
  4.     private Dictionary<T, Int16> dict;
  5.  
  6.     // code has been edited out of this example
  7.     // see further on in the question for the full class
  8.  
  9.     public IEnumerator<T> GetEnumerator()
  10.     {
  11.         throw new NotImplementedException();
  12.     }
  13.  
  14.     IEnumerator<T> IEnumerable<T>.GetEnumerator()
  15.     {
  16.         return dict.GetEnumerator();
  17.     }
  18. }
  19.        
  20. 'HashSet2<T>' does not implement interface member
  21. 'System.Collections.IEnumerable.GetEnumerator()'.
  22. 'HashSet2<T>.GetEnumerator()' cannot implement
  23. 'System.Collections.IEnumerable.GetEnumerator()'
  24. because it does not have the matching return type of
  25. 'System.Collections.IEnumerator'
  26.        
  27. public class HashSet2<T> : ICollection<T>
  28. {
  29.     private Dictionary<T, Int16> dict;
  30.     // Dictionary<T, bool>
  31.  
  32.     public HashSet2()
  33.     {
  34.         dict = new Dictionary<T, short>();
  35.     }
  36.  
  37.     public HashSet2(HashSet2<T> from)
  38.     {
  39.         dict = new Dictionary<T, short>();
  40.         foreach (T n in from)
  41.             dict.Add(n, 0);
  42.     }
  43.  
  44.     public void Add(T item)
  45.     {
  46.         // The key of the dictionary is used but not the value.
  47.         dict.Add(item, 0);
  48.     }
  49.  
  50.     public void Clear()
  51.     {
  52.         dict.Clear();
  53.     }
  54.  
  55.     public bool Contains(T item)
  56.     {
  57.         return dict.ContainsKey(item);
  58.     }
  59.  
  60.     public void CopyTo(
  61.         T[] array,
  62.         int arrayIndex)
  63.     {
  64.         throw new NotImplementedException();
  65.     }
  66.  
  67.     public bool Remove(T item)
  68.     {
  69.         return dict.Remove(item);
  70.     }
  71.  
  72.     public System.Collections.IEnumerator GetEnumerator()
  73.     {
  74.         return ((System.Collections.IEnumerable)
  75.             dict.Keys).GetEnumerator();
  76.     }
  77.  
  78.     IEnumerator<T> IEnumerable<T>.GetEnumerator()
  79.     {
  80.         return ((IEnumerable<T>)
  81.             dict.Keys).GetEnumerator();
  82.     }
  83.  
  84.     public int Count
  85.     {
  86.         get {return dict.Keys.Count;}
  87.     }
  88.  
  89.     public bool IsReadOnly
  90.     {
  91.         get {return false;}
  92.     }
  93. }
  94.        
  95. public IEnumerator GetEnumerator()
  96. {
  97.     return ((IEnumerable)dict.Keys).GetEnumerator();
  98. }
  99.  
  100. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  101. {
  102.     return ((IEnumerable<T>)dict.Keys).GetEnumerator();
  103. }
  104.        
  105. public IEnumerator GetEnumerator()
  106. {
  107.     dict.Keys.GetEnumerator();
  108. }
  109.  
  110. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  111. {
  112.     return dict.Keys.GetEnumerator();
  113. }
  114.        
  115. public IEnumerator GetEnumerator()
  116. {
  117.     return (IEnumerator)dict.Keys.GetEnumerator();
  118. }
  119.  
  120. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  121. {
  122.     return (IEnumerator<T>)dict.Keys.GetEnumerator();
  123. }