Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better to use array or List<>? [duplicate]

I was wondering which type would have better performance and which you think should be used.

For example I have a List of strings not knowing how many items I will need so having the .Add(String) function is really convenient. I can Add new strings to the list at any time easily.

What are the advantages/disadvantages of using each?

Are lists the new arrays?

like image 863
Gage Avatar asked Jun 04 '10 15:06

Gage


People also ask

Is it better to use arrays or lists?

Arrays take up memory more efficiently than lists.

Which one is faster array or List?

An Array is a collection of similar items. Whereas ArrayList can hold item of different types. An array is faster and that is because ArrayList uses a fixed amount of array.

In what situation should you use a List instead of an array of T?

List occupies more memory than Array since each node defined the List has its own memory set. Array memory is static and continuous. Arrays are memory efficient. A list is useful when elements are frequently added and removed.

Is it better to use array or List C#?

In general, it's better to use lists in C# because lists are far more easily sorted, searched through, and manipulated in C# than arrays. That's because of all of the built-in list functionalities in the language.


2 Answers

More context is really required to answer the question properly:

In a public API, you should try to use abstract collection types, so that you can change the internal implementation later if you need to.

  • If the collection should not be changed by the outside world, use IEnumerable<T>.
  • If the collection will be changed by the outside world, use ICollection<T>.
  • If indexed access is required, use IList<T>.

In a private implementation, it's not as important to use the abstract types:

  • If you need indexed access and know the final size, use T[] or List<T>.
  • If you need indexed access and don't know the final size, use List<T>.
  • If you plan to access elements in a LIFO pattern, use Stack<T>.
  • If you plan to access elements in a FIFO pattern, use Queue<T>.
  • If you need to access elements at the beginning and end of the list, but not in the middle, use LinkedList<T>.
  • If you don't want duplicates, use HashSet<T>.

In .NET 4.0 you have a few more choices, but those are the basics.

like image 142
Aaronaught Avatar answered Oct 01 '22 19:10

Aaronaught


List<String> is implemented using an array String[].

If you don't know how many elements you'll have, use List<String>

You can give the estimated (or maximum) number of elements you expect in the capacity constructor parameter (new List<String>(10)), this will be the initial size of the underlying array.

When you Add() an item and there is no room for this item, the underlying array is copied to a new array of double the size.

What I do: when I know the exact size of the collection and I know I won't change the size of the collection, I use an array (String[]). Otherwise I use a List<String>.

By the way, this goes for any type and not just String.

like image 42
brickner Avatar answered Oct 01 '22 17:10

brickner