Edit

Share via


Compiler Error CS0230

Type and identifier are both required in a foreach statement

A foreach statement was poorly formed.

The following sample generates CS0230:

C#
// CS0230.cs  
class MyClass  
{  
   public static void Main()  
   {  
      int[] myarray = new int[3] {1,2,3};  
  
      foreach (int in myarray)   // CS0230  
      {  
         Console.WriteLine(x);  
      }  
   }  
}  

and the sample below presents the same code, but with no CS0230 error:

C#
class MyClass
{
   public static void Main()
   {
      int[] myarray = new int[3] {1,2,3};

      foreach (int x in myarray)  // Both type (int) and indentifier (x) are specified
      {
         Console.WriteLine(x);
      }
   }
}