6

when is it appropriate to use an array over a Generic List(of T) in .NET

I have struggled to see any benefit an array provides over the Generic List but get the feeling I may be missing something. Performance is one thing that struck me as one potential bottleneck ?

Thanks

Dean
  • 5,896
  • 12
  • 58
  • 95

5 Answers5

3

When you are using a fixed number of a given type of object there's no point in taking the small performance hit associated with the List class as you won't use any of its features.

Garry Shutler
  • 32,260
  • 12
  • 84
  • 119
3

I will now shamelessly copypaste my edited answer for a very similar question asked here today:

I would like to point it out that while arrays have covariance, generic lists do not. For example, an array of type MyChildClass[] can be easily casted to MyParentClass[], while List<MyChildClass> cannot be casted to List<MyParentClass>, at least not directly.

If you need covariance, either use arrays, use LINQ's Cast() method or some other means to cast each item individually.

Community
  • 1
  • 1
Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
  • or wait for C#4 which will have covariance;-) – JoshBerke Jan 05 '09 at 14:14
  • Josh: As Mark Gravell pointed out in a comment on my original answer: "C# 4 will not offer covariance for lists - only IEnumerable (and a few delegates) - it demands strict "in" *xor* "out" usage, and List has both "in" *and* "out" usage." – Tamas Czinege Jan 05 '09 at 14:21
  • Interesting didn't know that. One issue with Arrays as Covariance is if you have MyParentClass[] set to MyChildClass[], if you then try and put MySecondChild class into MyParentClass you'll get an exception. Not very intuitive. – JoshBerke Jan 05 '09 at 15:24
  • Here's more info about array covariance: http://blogs.msdn.com/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx – JoshBerke Jan 05 '09 at 15:24
2

Another reason to use regular arrays, is when you are working with platform invoke (execute unmanaged c/c++ code), but this is limited to a very small set of applications

Also, as a remark: if the size of the array is known but you still want to use a generic list, don't forget to pass the size to the capacity parameter of the constructor.

for example:

List<int> lst = new List<int> (100);

Otherwise, the list will start with a very small capacity, and will several times need to allocate new chunks of memory, instead of allocating the required space at once.

bmotmans
  • 15,650
  • 5
  • 20
  • 14
0

Many of the higher level data structures (including List) use arrays internally, it's the only way to store a raw sequence in memory - so array have to exist in the programing language, otherwise you wouldn't have higher level data structures.

My opinion is that unless you have to (for example because you are using a component that requires it) you should never use arrays - especially don't write code that returns arrays are accept arrays as parameters, so you don't force others to use arrays too.

The only exception (in my opinion) is the rare case you need a collection of a known fixed size as a local variable than it's just seems wasteful to use something else.

Nir
  • 29,306
  • 10
  • 67
  • 103
0

If you want to cache a fixed number of data, use arrays. Otherwise, use Generic lists...they are just easier to use, provide more flexibility, and are typed (no need for boxing/unboxing)...simple as that. Also, an array is a fixed length while lists can be variable and change whenever you like. The reasons can go on and on why to use a generic list vs. an array. Arrays are just a pain in the ass. And honestly the performance hit using a list vs. an array can probably be debated to the nth degree