Computer Science, asked by meghachadha3117, 1 year ago

How do you use ‘foreach’ loop for iterating over an array in C#?

Answers

Answered by deo42
0

// C# program to illustrate the

// use of foreach loop

using System;

 

class GFG {

 

   // Main Method

   static public void Main()

   {

 

       Console.WriteLine("Print array:");

 

       // creating an array

       int[] a_array = new int[] { 1, 2, 3, 4, 5, 6, 7 };

 

       // foreach loop begin

       // it will run till the

       // last element of the array

       foreach(int items in a_array)

       {

           Console.WriteLine(items);

       }

   }

}

Output:

Print array:

1

2

3

4

5

6

7

Explanation: foreach loop in above program is equivalent to:

for(int items = 0; items < a_array.Length; items++)

{

   Console.WriteLine(items);

}

Similar questions