ArrayList.AddRange()

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

    #1

    ArrayList.AddRange()

    Hi,

    I was wondering why it is that when adding a collection of controls to an
    array list, you must use AddRange, instead of Add.

    Thanks in advance for any ideas

    Ant
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: ArrayList.AddRa nge()

    Ant,

    Well, if you are calling Add, you are adding the collection itself as
    the single element to the arraylist. If you are calling AddRange, then you
    are going to add all the elements of the collection individually to the
    list.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Ant" <Ant@discussion s.microsoft.com wrote in message
    news:190486A8-F024-4C01-920C-D3D07B39F641@mi crosoft.com...
    Hi,
    >
    I was wondering why it is that when adding a collection of controls to an
    array list, you must use AddRange, instead of Add.
    >
    Thanks in advance for any ideas
    >
    Ant

    Comment

    • Ant

      #3
      Re: ArrayList.AddRa nge()

      Thanks that makes sense, however, if I add a collection of say, strings, or
      an array of numbers, I can actually iterate through them as individual
      elements, not so with a collection of controls. Why the difference?

      Thanks for your thoughts.

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      Ant,
      >
      Well, if you are calling Add, you are adding the collection itself as
      the single element to the arraylist. If you are calling AddRange, then you
      are going to add all the elements of the collection individually to the
      list.
      >
      Hope this helps.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Ant" <Ant@discussion s.microsoft.com wrote in message
      news:190486A8-F024-4C01-920C-D3D07B39F641@mi crosoft.com...
      Hi,

      I was wondering why it is that when adding a collection of controls to an
      array list, you must use AddRange, instead of Add.

      Thanks in advance for any ideas

      Ant
      >
      >
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ArrayList.AddRa nge()

        Ant <Ant@discussion s.microsoft.com wrote:
        Thanks that makes sense, however, if I add a collection of say, strings, or
        an array of numbers, I can actually iterate through them as individual
        elements, not so with a collection of controls. Why the difference?
        It's more descriptive (and simpler) in your code to call AddRange than
        to iterate through.

        The same can be said of many things - you could do without
        String.Substrin g, using a StringBuilder to append each of the
        characters you're interested in - but it would make life considerably
        messier.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • ktrvnbq02@sneakemail.com

          #5
          Re: ArrayList.AddRa nge()

          Jon wrote:
          It's more descriptive (and simpler) in your code to call AddRange than
          to iterate through.
          In addition an AddRange() method often has better performance than
          repeatedly calling an Add() method, as the class can often optimise its
          access and manipulation of internal data structures (e.g. if it needs
          to reallocate an array it can be done in one hit), and you're making
          less method calls overall.


          Matt

          Comment

          Working...