G
garyusenet
I'm a bit confused about the differences of these two commands (what is
the right word for commands here?) when used to enumerate the contents
of an array.
The below example uses both foreach and for to enumerate the contents
of the array.
Also as well as explaining the differences could you explain why the
foreach messagebox isn't working below.
Many TIA.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace array_examples
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
int[] intarray = new int[] { 5, 6, 7, 8 };
MessageBox.Show("This example uses for");
for (int i = 0; i < intarray.Length; i++)
{
MessageBox.Show(intarray.ToString());
}
MessageBox.Show("While this example uses foreach");
foreach (int y in intarray)
{
MessageBox.Show(intarray[y].ToString());
}
}
}
}
the right word for commands here?) when used to enumerate the contents
of an array.
The below example uses both foreach and for to enumerate the contents
of the array.
Also as well as explaining the differences could you explain why the
foreach messagebox isn't working below.
Many TIA.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace array_examples
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
int[] intarray = new int[] { 5, 6, 7, 8 };
MessageBox.Show("This example uses for");
for (int i = 0; i < intarray.Length; i++)
{
MessageBox.Show(intarray.ToString());
}
MessageBox.Show("While this example uses foreach");
foreach (int y in intarray)
{
MessageBox.Show(intarray[y].ToString());
}
}
}
}