.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

The 'foreach' Loop in C#

Posted By:Abhisek Panda       Posted Date: November 20, 2009    Points: 10    Category: OOPS    URL: http://www.dotnetspark.com  

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 ={"DotNetSpark","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'.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend