This program explains how to use foreach loop. The foreach loop uses collection of items and display one by one.
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ForeachinCSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private voidbutton1_Click(object sender, EventArgs e)
{
// Example 1
string[] str = {“C”,“#”,“Programming”};
foreach (stringk in str)
{
MessageBox.Show(“Dot Net “ + k);
}
// Example 2
string[] Month = { “Jan”, “Feb”,
“Mar”,“Apr”,“May”,“June”,“July”,“Aug”,
“Sept”,“Oct”,“Nov”,“Dec”};
foreach (stringi in Month)
{
MessageBox.Show(“The Month is : “ + i);
}
}
}
}