Nigel Foster

Nigel Foster

  • NA
  • 9
  • 502

Iterating through an array using ForEach

Jan 28 2018 11:28 AM
Hello.  Brilliant forum by the way.  Why doesn't this work please?
  1.         public void InitializePack()  
  2.         {  
  3.             Card [] pack=new Card[52];  
  4.             for (int i = 0; i < 4; i++) {  
  5.                 for (int j = 1; j < 14; j++) {  
  6.                     pack[(i*13)+j-1] = new Card((Suits)i, (Values)j);  
  7.                 }  
  8.             }  
  9.             foreach (Card c in pack)  
  10.                 Console.WriteLine(c.Name);   
  11.         } 
c is null at first iteration.  I have only seen examples of foreach iterating through arrays whose elements are defined at declaration using {}.  Is this the issue?
 
I've not included the rest of the code as I thought you'd see this one straight away. 
 
Thanks very much! 

Answers (5)