Hi
I have a code here composed of Array. I would like to know what does this mean here: (please see last 3 lines)
What does it mean?

Thanks & regards

         ArrayList numbers = new ArrayList();
        string yourValue;

        Console.WriteLine("Give the value (b to end)");
        Console.WriteLine();

        do
        {
            Console.WriteLine("Give the value:");
            yourValue = Console.ReadLine();

            if (yourValue == "b")
                break;
            numbers.Add(Convert.ToInt32(yourValue));
        }
        while (true);
        System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
        while (Enumerator.MoveNext())
            Console.WriteLine(" " + Enumerator.Current);

Recommended Answers

All 8 Replies

this will loop through all the values in your array
IEnumerator basically means you can use it to iterate your data

lets say you have 1, 5, 3, 8 in your array

this will print out

1 5 3 8

so in looping all the valus I always need this one:
System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
while (Enumerator.MoveNext())
Console.WriteLine(" " + Enumerator.Current);

is that right?

thanks

you don't always have to use that way, but you can

you could also do something like this

foreach(int num in numbers)
{
  Console.WriteLine(" " + num);
}

i tried this but when I write the "b" to end, I got an error. this is what I've done.

while (true);
            //this will loop through all the values in your array IEnumerator basically means you can use it to 
            //iterate your data
            foreach (int num in numbers)
            {
                Console.WriteLine(" " + num);
            }


            
            //System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
            //while (Enumerator.MoveNext())
            //    Console.WriteLine(" " + Enumerator.Current);
        }

its because you are using an untyped list for one, but you didn't have an else statement

if (yourValue == "b")
{
break;
}
else
{
numbers.Add(Convert.ToInt32(yourValue));
}

also you could use List instead of ArrayList

List<int> numbers = new List<int>();


also you could use List instead of ArrayList

List<int> numbers = new List<int>();

You don't really need the typed List for what's going on, it's just a nice thing to have in terms of type safety... :)

ok i'll try that again and will publish the results.
Thanks;-)

hi
thanks! it works-)
also the List<> i tried and it also works
have a good day;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.