IEnumerator and arraylist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • starter

    IEnumerator and arraylist


    I am trying to learn IEnumerator and collections(Arr ayList, Hashtable)
    in .NET.
    Can anyone tell me what is IEnumerator used for and any online
    resources would
    be helpful.

    Thanks

  • Vadym Stetsyak

    #2
    Re: IEnumerator and arraylist

    As you can see from the name of interface it is designated for enumaration
    that is iteration of elements of container.

    ArrayList in your situation can be considered container

    to iterate over it following code can be used

    ArrayList al = new ArrayList();
    al.Add("str1");
    al.Add("str2");
    al.Add("str3");
    foreach(string str in al)
    {
    Console.WriteLi ne(str);
    }

    If you have .NET 2.0 I'd recommed you use Generics to avoid boxing, when
    using value types

    When you implement IEnumerator in your container then foreach statement can
    be used to enumerate the contents of container.

    sample of IEnumerator implementation
    (
    http://www.c-sharpcorner.com/code/20...InnerClass.asp )

    --
    Vadym Stetsyak aka Vadmyst
    Blog about software development, algorithms, network protocols, .NET, programming languages, tips & tricks, coding techniques and more.


    "starter" <enlight.v@gmai l.com> wrote in message
    news:1138525286 .222903.199470@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    >
    > I am trying to learn IEnumerator and collections(Arr ayList, Hashtable)
    > in .NET.
    > Can anyone tell me what is IEnumerator used for and any online
    > resources would
    > be helpful.
    >
    > Thanks
    >[/color]


    Comment

    • Kevin Spencer

      #3
      Re: IEnumerator and arraylist

      See the following resources:




      --
      HTH,

      Kevin Spencer
      Microsoft MVP
      ..Net Developer
      Who is Mighty Abbott?
      A twin turret scalawag.

      "starter" <enlight.v@gmai l.com> wrote in message
      news:1138525286 .222903.199470@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      >
      > I am trying to learn IEnumerator and collections(Arr ayList, Hashtable)
      > in .NET.
      > Can anyone tell me what is IEnumerator used for and any online
      > resources would
      > be helpful.
      >
      > Thanks
      >[/color]


      Comment

      • anonieko@hotmail.com

        #4
        Re: IEnumerator and arraylist

        Basic things first about interface.

        Think of interface to mean 'feature list' or 'list of capabilities'.

        When a class implements an interface, it actually means that class
        will have those 'features' or 'capabilities' listed in that interface.
        But this time, those features are not just listed but instead are
        actually coded ( implemented in code).

        If you get to understand this. You will see that

        IEnumerator is an inteface that gives the ability to work with a
        collection of objects. It has the 'list of features' so that you can
        go through each object in the collection. The IEnumerator simply
        specifies what are the things you need to do this. If you look at
        the documentation is has these members

        1. Current - pick current object
        2. MoveNext - go to the next object
        3. Reset - go back start all over again

        Hey, think of it. Those simple three are just ENOUGH to get things
        done. There is nothing more you need!!!!! That's serves exactly the
        purpose of the interface.


        Ok so much for the blah blah. Actual code now!

        This article discusses and compares both versions in NET 1.X and .NET
        2.0




        ---- VERSION 1.X

        class IteratorTest : IEnumerable
        {
        private ArrayList items = new ArrayList();

        public int Count
        {
        get { return items.Count; }
        }

        public object this[int index]
        {
        get { return items[index]; }
        set { items.Insert(in dex, value); }
        }

        public IEnumerator GetEnumerator()
        {
        return new IteratorSampleE numerator(this) ;
        }

        private class IteratorSampleE numerator : IEnumerator
        {
        IteratorTest aggregate = null;
        int index = -1;

        public IteratorSampleE numerator(Itera torTest aggregate)
        {
        this.aggregate = aggregate;
        }

        public virtual object Current
        {
        get { return this.aggregate[index]; }
        }

        public virtual bool MoveNext()
        {
        index++;
        if (index >= this.aggregate. Count)
        {
        return false;
        }
        else
        {
        return true;
        }
        }

        public virtual void Reset()
        {
        index = -1;
        }
        }




        --------- VERSION 2.0

        class IteratorTest2<T > : IEnumerable
        {
        private ArrayList items = new ArrayList();

        public int Count
        {
        get { return items.Count; }
        }

        public T this[int index]
        {
        get { return (T)items[index]; }
        set { items.Insert(in dex, value); }
        }

        public IEnumerator GetEnumerator()
        {
        foreach (T item in this.items)
        {
        yield return item;
        }
        }
        }

        Comment

        Working...