c# array vs generic list [duplicate]
Asked Answered
H

7

33

i basically want to know the differences or advantages in using a generic list instead of an array in the below mentioned scenario

class Employee
{
    private string _empName;

    public string EmpName
    {
       get{ return _empName; }
       set{ _empName = value; }
    }
}

1. Employee[] emp 
2. List<Employee> emp

can anyone please tell me the advantages or disadvantages and which one to prefer?

Hexachlorophene answered 12/3, 2010 at 6:18 Comment(1)
Your class would be cleaner if you used auto-Implemented Properties: public string EmployeeName {get; set;}. Using full names like EmployeeName would help also. Oh, and how about some types too? :-)Sloat
L
21

One big difference is that List<Employee> can be expanded (you can call Add on it) or contracted (you can call Remove on it) whereas Employee[] is fixed in size. Thus, Employee[] is tougher to work with unless the need calls for it.

Lustrum answered 12/3, 2010 at 6:21 Comment(2)
Plus with the LINQ libraries, you can always call List.ToArray() if you do at some point NEED it in an array somewhere, so the List really gives you the best power and flexibility.Furl
ToArray() is a built-in method of the List object that predates LINQ. LINQ adds a similar method to IEnumerables.Agalloch
T
16

The biggest difference is that arrays can't be made longer or shorter once they're created. List instances, however can have elements added or removed. There are other diffs too (e.g. different sets of methods available) but add/remove is the big difference.

I like List unless there's a really good reason to use an Array, since the flexibility of List is nice and the perf penalty is very small relative to the cost of most other things your code is usually doing.

If you want to dive into a lot of interesting technical detail, check out this StackOverflow thread which delves into the List vs. Array question in more depth.

Taiga answered 12/3, 2010 at 6:23 Comment(3)
That doesn't make them immutable; you can still swap contents. It just makes them fixed-size.Morice
Erk, good point Mark. Was mis-using the term immutable. Changing now. BTW, I see you've answered this before... I added a link to your answer above. :-)Taiga
+1 for link to the other threadFabulist
M
7

With the generic list, you can Add / Remove etc cheaply (at least, at the far end). Resizing an array (to add/remove) is more expensive. The obvious downside is that a list has spare capacity so maybe wastes a few bytes - not worth worrying about in most cases, though (and you can trim it).

Generally, prefer lists unless you know your data never changes size.

API-wise, since LINQ there is little to choose between them (i.e. the extra methods on List<T> are largely duplicated by LINQ, so arrays get them for free).

Another advantage is that with a list you don't need to expose a setter:

private readonly List<Foo> items = new List<Foo>();
public List<Foo> Items { get { return items; } }

eliminating a range of null bugs, and allowing you to keep control over the data (especially if you use a different IList<> implementation that supports inspection / validation when changing the contents).

Morice answered 12/3, 2010 at 6:22 Comment(0)
M
2

You need to know the size of an array at the time that it is created, but you cannot change its size after it has been created.

So, it uses dynamic memory allocation for the array at creation time. (This differs from static memory allocation as used for C++ arrays, where the size must be known at compile time.)

A list can grow dynamically AFTER it has been created, and it has the .Add() function to do that.

-from MSDN

  1. Generics Vs Array Lists-SO General comparision.
  2. Generic List vs Arrays-SO Why is generic list slower than array?

Which one to prefer? List<T>.

Melisandra answered 12/3, 2010 at 6:22 Comment(0)
W
2

If you are exposing a collection in a public interface the .NET Framework Guidelines advise to use a List rather than T[]. (In fact, a BindingList< T >)

Internally, an array can be more appropriate if you have a collection which is a fixed, known size. Resizing an array is expensive compared to adding an element to the end of a List.

Windlass answered 12/3, 2010 at 6:23 Comment(0)
C
1

If you know the number of elements array is a good choice. If not use the list. Internally List<T> uses an array of T so the are actually more like than you may think.

Consuetudinary answered 12/3, 2010 at 6:22 Comment(0)
F
1

With a List, you don't need to know the size of the array beforehand. You can dynamically add new Employee's based on the needs of your implementation.

Falsify answered 12/3, 2010 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.