Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
What is the difference between List<string> and ArrayList. Is there any performance issue?
Posted
Updated 29-Jun-11 1:40am
v3

Not much I would think - since there is no boxing / unboxing here.
Implementing List<t> might just still come out better in terms of performance because there is no need to do any type checking at runtime.

Two more interesting reads on this topic -
http://blogs.msdn.com/b/joshwil/archive/2004/04/13/112598.aspx[^]
http://blog.drorhelper.com/2008/10/why-should-i-use-list-and-not-arraylist.html[^]
 
Share this answer
 
Comments
Tarun.K.S 29-Jun-11 8:00am    
How come there is no boxing/unboxing in ArrayList?
Abhinav S 29-Jun-11 8:13am    
The OP has compared List<string> with ArrayList. Since a string is already on the heap, there will be no boxing.
If the user was talking about comparing List<int> with an ArrayList there would be more performance benefit.
Tarun.K.S 29-Jun-11 8:27am    
Aah right, OP is comparing List<String> and ArrayList. Uff, let me get myself a cup of coffee!
Abhinav S 29-Jun-11 9:21am    
:)
#realJSOP 29-Jun-11 8:40am    
Generally speaking, though, he should avoid using an ArrayList whenever possible. It's simply not necessary in about 99.999% of all cases.
The difference is that you perform boxing/unboxing (casting the string to object and vice verca) with ArrayList, and generic List<string> allows you to avoid it. Performance of generic classes is better.

And that's what Google came up with.[^]
 
Share this answer
 
v2
Comments
Tarun.K.S 29-Jun-11 7:58am    
хороший ответ! 5!
Sergey Alexandrovich Kryukov 29-Jun-11 12:19pm    
Wrong! See my comment.
--SA
Tarun.K.S 29-Jun-11 12:59pm    
Aah yes even I realised the mistake after I saw Abhinav's comment. It was due to lack of coffee! LOL!
Tarun.K.S 29-Jun-11 13:02pm    
A little off-topic, what is the meaning of your first name? Like meaning of my first name Tarun is "younger"(indeed I do look young!) :)
Dima Popov 29-Jun-11 15:27pm    
Dima is short variant of Dmitry, which is a slightly modified Greek name. So it doesn't seem to have any meaning in Russian, though initially had one. There's an article on Wikipedia: http://en.wikipedia.org/wiki/Dmitry. What I can tell you is that it's a very common name. Russian names that do have decent meaning are quite rare.
A generic List is more efficient because it deals with just the specified object type. An ArrayList requires boxing because it supports multiple types in a single list. It's almost always better to use a List rather than an ArrayList.
 
Share this answer
 
Comments
Tarun.K.S 29-Jun-11 7:58am    
Right.
Sergey Alexandrovich Kryukov 29-Jun-11 12:19pm    
Sorry, not exactly so. String is "always boxed" anyway. As boxing/unboxing does not takes place for this type, there is no performance leak. But if the user does "string myString = myObject as string", it will eat up time.
--SA
ArrayLists are essentially deprecated as they're untyped - you need to use casts with them - and they're slower and less space efficient for value types because they require the items to be boxed.

Generic lists were introduced with .Net 2.0 and are the way to go. Often a List is better than an array, with few downsides.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jun-11 12:23pm    
Not using ArrayList is a good point. Indeed, it should ***never*** be used.

As to space and speed -- not quite so. More exactly -- it depends. It depends on the type of the element (if boxing/unboxing needed) and on how the user does the case. Please see my comments to other answers.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900