title: Why would I use a HashSet over a Dictionary?
body: <p>I'm trying to implement a list of cached paths on a A* algorithm. Currently, the cached paths are stored in a list like this:</p>
  
  <pre><code>readonly List&lt;CachedPath&gt; _cachedPaths = new List&lt;CachedPath&gt;();
  </code></pre>
  
  <p>The operations performed over this list are:</p>
  
  <p>FirstOrDefault to get an element that satisfies certain conditions</p>
  
  <pre><code>var cached = _cachedPaths.FirstOrDefault(p =&gt; p.From == from &amp;&amp; p.To == target &amp;&amp; p.Actor == self);
  </code></pre>
  
  <p>Remove and element</p>
  
  <pre><code>_cachedPaths.Remove(cached);
  </code></pre>
  
  <p>Additions</p>
  
  <pre><code>_cachedPaths.Add(new CachedPath {
                      From = from,
                      To = target,
                      Actor = self,
                      Result = pb,
                      Tick = _world.WorldTick
                  });
  </code></pre>
  
  <p>NOTE: The class CachedPath has GetHashCode and Equals overriden by just the From, To and Actor, so two instances that have these same attributes have the same hash and equality.</p>
  
  <p>Given that quick lookups (Contains), insertions and deletions in a 'HashSet' are O(1) (if I'm not mistaken), I considered using a 'HashSet' to do these operations. The only problem is the FirstOrDefault, that I had to enumerate the whole collection to get it.</p>
  
  <p>Given this problem, I considered also using a Dictionary indexed by the hash of From, To and Actor:</p>
  
  <pre><code>Dictionary&lt;int, CachedPath&gt; cachedPath
  </code></pre>
  
  <p>Once again, if I'm not mistaken, Dictionary also offers O(1) in insertions, deletions, and also retrieval by Key. This leads me to think that a Dictionary is a HashSet + O(1) element retrieval capabilities.</p>
  
  <p>Am I missing something? Is really Dictionary better than HashSet in the sense that it supports more operations?</p>
  
  <p>Thanks in advance.</p>