title: C# Create a deck of Cards using foreach
body: <p>This is a homework assignment FYI. And thanks for at least looking at this. </p>
  
  <p>So we're learning about <code>enums</code>, <code>List&lt;&gt;</code>, <code>IComparable&lt;T&gt;</code> and <code>structs</code>, and we're creating a deck of cards. I have a Card Struct, that says to create a card, it needs a rank and suit. I have enums for both. </p>
  
  <p>I'm having trouble with the NewDeck() method. </p>
  
  <p>We were given the method header to use, and given a TIP to "use the static method GetValues to loop through all Rank Enumerators". </p>
  
  <p>We were also given the foreach loop.  We're not supposed to call a sort, because looping through all the ranks should create a list already sorted. </p>
  
  <p>I'm having trouble knowing how to add Cards to the deck. To add the Card, I would think I might use Card.Add, but it's not showing up in code completion, and it's not an option, and I'm not sure why. Here's the code for my Cards Struct-</p>
  
  <pre><code>    namespace Cards
  {
      struct Card : IComparable&lt;Card&gt;
      {
          public Suit Suit { get; private set; }
          public Rank Rank { get; private set; }
          public Card(Suit suit, Rank rank) :
              this()
          {
              this.Suit = suit;
              this.Rank = rank;
          }
          public override string ToString()
          {
              return string.Format("{0} {1}", (Char)this.Suit, this.Rank);
          }
  
          public int CompareTo(Card other)
          {
              return Rank.CompareTo(other.Rank);
          }
      }
  
      public enum Suit { Spades = 9824, Clubs = 9827, Diamonds = 9830, Hearts = 9829 }
  
      public enum Rank { Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
  }
  </code></pre>
  
  <p>And here's the code for my newDeckMethod()</p>
  
  <pre><code>public static List&lt;Card&gt;  NewDeck()
          {
              foreach (Cards.Rank r in Enum.GetValues(typeof(Rank)))
                 //Card.Add           
  
          }
  </code></pre>