Compare a List with an Array (in .NET)

Compare a List with an Array (in .NET)

Please list the pros and cons of each.

Thanks
curiouswebsterSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Both are type safe.  A List has methods that are used to control the internal structure (Add, Remove, Insert, InsertAt, etc.) where as arrays have to be manipulated by index.  It should be noted, however, that a List's internal collection *is* an array.

They are both Enumerable's meaning that you can use LINQ when working with them.

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nikoloz KhelashviliSoftware DeveloperCommented:
Here is useful threads for you

Array versus List<T>: When to use which?

Should I use a list or an array?

I would always use List, unless any method is expecting array as param
curiouswebsterSoftware EngineerAuthor Commented:
I thought that an ArrayList automatically up-typed anything you add to it to type Object, and used boxing and unboxing to do so. This means the compiler does not enforce the type you pass in, like a generic list List<> does for you at compile time.

Secondly, an Array (versus an ArrayList) seems even more un-disciplined. It seems you can pile in data of any type, and even more arrays. It then is the job of the programmer to query that array before addressing a cell directly.
kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
I thought that an ArrayList automatically up-typed anything you add to it to type Object, and used boxing and unboxing to do so.
ArrayList is not strongly typed, no. Everything goes into it as type object. Only value types are boxed/unboxed. Nobody these days suggests using an ArrayList for anything. Generic collections are the modus operandi.

This means the compiler does not enforce the type you pass in, like a generic list List<> does for you at compile time.
Correct.

It seems you can pile in data of any type, and even more arrays. It then is the job of the programmer to query that array before addressing a cell directly.
Arrays are strongly typed. If you declare an array of ints, you can't subsequently stick a string into one of its slots. Yes it is the job of the programmer to be able to retrieve things from the list, as the developers of the List class themselves had to do (since a List uses an array under the hood).
curiouswebsterSoftware EngineerAuthor Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.