title: Foreach in a Hashset
body: <p>I have a structure which can't contain duplicates, so I've chosen to represent it as a hashset  </p>
  
  <pre><code>class Team
  {
      private HashSet&lt;HeroStats&gt; team = new HashSet&lt;HeroStats&gt;();
  
      public int count()
      {
          int res = 0;
          res = team.Count;
          return res;
      }
  
      public Team()
      {
      }
  
      public void add(HeroStats hero)
      {
          if (team.Count &lt;= 5)
              team.Add(hero);
          else
          Console.WriteLine("Team is full , create another team");
       }
  
      public List&lt;HeroStats&gt; print() {
          List&lt;HeroStats&gt; l = new List&lt;HeroStats&gt;();
          foreach (HeroStats h in team)
              l.Add(h);
          return l;
      }
  }
  </code></pre>
  
  <p>In another class i want to do a foreach on an object of type Team</p>
  
  <pre><code>public void fitness(Team t)
      {
  
          int score;
          foreach(HeroStats hero in t)
          {
          }
  
      }
  </code></pre>
  
  <p>How can I make the foreach function work without discarding the hashset ?</p>