The foreach Loop in C#

Posted by Abhisek under C# category on | Views : 2544
The 'foreach' Loop in C#:-

The C# 'foreach' keyword is used to iterate over all items within an array, without the need to test the upperbound of the array. See the below program,

class ForeachLoop
static void Main(string[] args)
//itterate string array
string[] name ={"DotNetSpider","Join it NOW!!"};
foreach(string s in name)
//itterating integer array
int[] value={12,23,34,45,56};
foreach(int i in values)

The above program will traverse the string and integer array. The 'foreach' loop can also be used to itterate over system-supplied or user-defined collections. The 'foreach' loop in C# is used to retrive the values, that means it is used for receiving values, we can't enter values using 'foreach'.

Comments or Responses

Login to post response