tags:

views:

726

answers:

8

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

+2  A: 

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
+7  A: 

I think that Eric Lippert says it better than I could:
http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx

Joel Coehoorn
bahh you beat me to it:-)
JoshBerke
VERY interesting article! Thank you!
Daniel Schaffer
+12  A: 

Read this article by Erik Lippert: http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx

You should probally never return an array from a public property or method.

JoshBerke
+1 for the same link as Joel, but you have less reputation and you provide more information with the post.
BeowulfOF
Actually the quote is: 'You probably should not return an array as the value of a public method or property'.
Gary Willoughby
Agree with Gary: NEVER is a strong word.
Joel Coehoorn
+3  A: 

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.

DrJokepu
or wait for C#4 which will have covariance;-)
JoshBerke
Josh: As Mark Gravell pointed out in a comment on my original answer: "C# 4 will not offer covariance for lists - only IEnumerable<T> (and a few delegates) - it demands strict "in" *xor* "out" usage, and List<T> has both "in" *and* "out" usage."
DrJokepu
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
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
+1  A: 

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
A: 

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
A: 

As far as we know, arrays are a primitive data structure, if you need to insert or delete elements on middle position, as well sorting it, normally in obsolete languages is need the programmer to do that, instead when you are working with a List object like DotNet Generics, all these operations are already done for you.

More than, arrays are much limited, only the same datatype elements, etc.

For present days of computation, I don't see advantage by usage of arrays.

Regards.

Carlos Eduardo Olivieri
A: 

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