Net-informations.com
SiteMap  | About    

foreach loop

The foreach loop executes a block of code on each element in an array or a collection of items. When executing foreach loop it traversing items in a collection or an array .

vb.net
  For Each [Item] In [Group]
   [loopBody]

Next [Item]

C#
  foreach(Item in Group){
	// loopBody

}

  Item : The Item in the group

Group : The group containing items

The following ASP.NET program add an array of item to a Listbox using for..each loop.

Default.aspx

  

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
Default.aspx.cs
Default.aspx.vb





net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.