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

Top 5 Contributors of the Month

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

What is foreach Statement

Posted By: PankajGupta     Posted Date: July 21, 2009    Points:2   Category :C#
What is foreach Statement ? Please tell me in detail.

Thank U

Author: Faizal             
Posted Date: July 21, 2009     Points: 5   

The foreach statement enumerates the elements of a collection, executing an embedded statement for each element of the collection.

foreach ( type identifier in expression ) embedded-statement

The type and identifier of a foreach statement declare the iteration variable of the statement. The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement. During execution of a foreach statement, the iteration variable represents the collection element for which an iteration is currently being performed. A compile-time error occurs if the embedded statement attempts to modify the iteration variable (by assignment or the ++ and -- operators) or pass the iteration variable as a ref or out parameter.

using System;
class Test
static void Main() {
double[,] values = {
{1.2, 2.3, 3.4, 4.5},
{5.6, 6.7, 7.8, 8.9}
foreach (double elementValue in values)
Console.Write("{0} ", elementValue);


Author: Akhil Raj       [Moderator]      
Posted Date: July 22, 2009     Points: 5   

check the following article for more details..


Author: Syed Shakeer Hussain             
Posted Date: July 22, 2009     Points: 5   

foreach loops:

syntax: foreach (variable1 in variable2) statement[s]

The 'foreach' loop is used to iterate through the values contained by any object which implements the IEnumerable interface. When a 'foreach' loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2. As we have seen previously, such loops can be used to access array values. So, we could loop through the values of an array in the following way:

1.int[] a = new int[]{1,2,3};
2.foreach (int b in a)

Author: varun bansal             
Posted Date: July 27, 2009     Points: 5   


foreach statement same like for loop. but only one differece is that. foreach statement execute no. of values contained by object like array, data table..


foreach( datarow dr in datatable tb)


varun bansal

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

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