Loading ...

how foreach loop works ?

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » how foreach loop works ?

how foreach loop works ?

Posts under the topic: how foreach loop works ?

Posted: 10/19/2010

Starter 1414  points  Starter
  • Joined on: 12/1/2008
  • Posts: 66

Hello experts,

I was reading in 1 of the books  yesterday. There I saw this code.This is a collection of objects in arraylist. in order to iterate the items they used the below code.

 

        ArrayList arr = new ArrayList();
        string s = "Hello I'm vij";
        arr.Add(50);
        arr.Add(s);
        arr.Add("Hi Im Sumit");
        arr.Add(new object());
        arr.Insert(2, "Hi Im Ajit");
       

        IEnumerator enumerator = arr.GetEnumerator();
        while (enumerator.MoveNext())
        {
           Response.Write(enumerator.Current);
           Response.Write("<br/>");
        }

 

It was also written that the above code works internally in foreach loop.  Is this how foreach loop works?

Thanks,

Sumit


Posted: 10/19/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

SumitArora said:

user="Sumit Arora"] Is this how foreach loop works?

yes


Page 1 of 1 (2 items)