Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when should I use List rather than Array. Is both of them give us same result? Please explain with example. Many thanks who will response.
Posted
Updated 14-Jan-19 18:52pm
v2

I am summarizing some great answers.

1. Refer- Array versus List<t>: When to use which?[^].
Quote:

It is rare, in reality, that you would want to use an array. Definitely use a List<t> any time you want to add/remove data, since resizing arrays is expensive. If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful.

List<t> offers a lot more functionality than an array (although LINQ evens it up a bit), and is almost always the right choice. Except for params arguments, of course ;-p

As a counter - List<t> is one-dimensional; where-as you have have rectangular (etc) arrays like int[,] or string[,,] - but there are other ways of modelling such data (if you need) in an object model.

See also:

How/When to abandon the use of Arrays in c#.net?[^]
Arrays, What's the point?[^]
That said, I make a lot of use of arrays in my protobuf-net[^] project; entirely for performance:

it does a lot of bit-shifting, so a byte[] is pretty much essential for encoding
I use a local rolling byte[] buffer which I fill before sending down to the underlying stream (and v.v.); quicker than BufferedStream etc
it internally uses an array-based model of objects (Foo[] rather than List<foo>), since the size is fixed once built, and needs to be very fast
But this is definitely an exception; for general line-of-business processing, a List<t> wins every time.

2. Refer- arraylist vs List<> in c#[^]
Quote:
Yes, pretty much. List<t> is a generic class. It supports storing values of a specific type without boxing to and unboxing from object. ArrayList simply stores object references. As a generic collection, it implements the generic IEnumerable<t> interface and can be used easily in LINQ (without requiring any Cast or OfType call).

ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List<t>. You shouldn't use ArrayList in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.


3. Refer Performance Guideline: Use Generics To Eliminate the Cost Of Boxing, Casting and Virtual calls[^] to know the performance related advantage of Generic List over Array List.
 
Share this answer
 
Comments
adriancs 25-Mar-17 9:56am    
thanks
Welcome :)
To to make a long story short : Lists are dynamically resizable, arrays are not.
It means you should use a List when you don't know the number of items you want to manipulate when declaring your object (array or list).
 
Share this answer
 
Array you need to set limit while declare & redim if need to increase limit after declaration

List(Of Type) no need to give limit & it is easy to add & remove elements
so, when you know the limit of eements will be fix then go with array else go with list

for more details visit...
Performance of Arrays vs. Lists[^]

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Member 10275841 23-Feb-14 22:13pm    
Well, List do use arrays to do this for you under the code.
One of the advantage List provide over array is that List is dynamic in nature where you can add/ remove items at runtime. Where else array is static in nature you need define array length at design time and you cannot make change its length at runtime.
So if you know the number of elements to be stored, you can go for array. Otherwise we can go for list.
 
Share this answer
 
v2

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