T
Tony Johansson
Hi!
The ArrayList support the ICollection and IEnumerable.
Below is three ways to iterate through a collection of an instance of an
ArrayList object.
I just find it's easier to iterate though a collection by using the for loop
or the foreach so my question is
when is it better or more appropriate to use the Enumerator.
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add("hej");
//Using the foor loop
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list);
}
//Using the foreach loop
foreach (object i in list)
{
Console.WriteLine(i);
}
//Using the Enumerator
IEnumerator enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current);
}
//Tony
The ArrayList support the ICollection and IEnumerable.
Below is three ways to iterate through a collection of an instance of an
ArrayList object.
I just find it's easier to iterate though a collection by using the for loop
or the foreach so my question is
when is it better or more appropriate to use the Enumerator.
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add("hej");
//Using the foor loop
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list);
}
//Using the foreach loop
foreach (object i in list)
{
Console.WriteLine(i);
}
//Using the Enumerator
IEnumerator enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current);
}
//Tony