error for foreach statement

 public int[] boardState; // State of the baord

  void Start () 
    {

   foreach(state in boardState) 
      {
 state = 0;
       }

    }

error CS0230: Type and identifier are both required in a foreach statement

       private var boardState: int[]; // State of the baord
       // Set board state to off
       boardState = new int[9];
       for (state in boardState) 
        {
      state = 0;
        }

the code in java script is working fine but when i am trying to adopt same code in c # the error is coming

CS0230: Type and identifier are both required in a foreach statement

so how to modify the above java script to work in c#.

1 Answer

1
foreach (int state in boardState)

or

foreach (var state in boardState)

I recommend the first one. I save var for when the type is either very long or abundantly clear.

error CS1656: Cannot assign to state' because it is a foreach iteration variable'

Sorry, I didn't even bother to look at what was in the loop. You can't use foreach. A for loop is required.