473,503 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList.AddRange()

Ant
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
Nov 3 '06 #1
4 6235
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]
- mv*@spam.guard.caspershouse.com

"Ant" <An*@discussions.microsoft.comwrote in message
news:19**********************************@microsof t.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

Nov 3 '06 #2
Ant
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]
- mv*@spam.guard.caspershouse.com

"Ant" <An*@discussions.microsoft.comwrote in message
news:19**********************************@microsof t.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


Nov 3 '06 #3
Ant <An*@discussions.microsoft.comwrote:
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.Substring, using a StringBuilder to append each of the
characters you're interested in - but it would make life considerably
messier.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 3 '06 #4
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

Nov 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2914
by: ORC | last post by:
Hi, How to create an ArrayList from a System.Array or inserting the System.Array into the ArrayList ? Thanks Ole
7
23516
by: MrNobody | last post by:
since ArrayList implements ICollection, is there a quick way to convert an ICollection into ArrayList (besides actually iterating through each element in the ICollection and explicitly adding them...
1
5724
by: Craig | last post by:
Hey Everyone - I'm trying to determine the fastest way of moving array information around and could use some help. Here's the setup: Class A stores a DateTime and an amount (a payment)....
1
5565
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
2
1372
by: RBCC | last post by:
I have a form with a 2 textboxes and a listbox, The data is read in by a file called "memberphones" The listnbox lists names in this way (lastname,first name) sorted by the last name. the...
2
6844
by: Russ Green | last post by:
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM. I have this code which I am trying to get to work... The key bits of information are ...
11
16416
by: pmclinn | last post by:
I have a function that returns an arraylist. I want the returned arraylist to be copied into the local arraylist. What is the most efficient way of doing this? -Peter
11
16235
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
4
1440
by: | last post by:
I have three Arraylists that hold strings corresponding to addresses of an e-mail list: ArrayList masterList ArrayList optInToMasterList ArrayList optOutFromMasterList I want to know the...
22
15902
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.