When to use Array () over Generic.Lists in .NET

when it is advisable to use an array in the general list (from T) in .NET.

I tried my best to see any benefit that the array provides in the General list, but I feel that I am missing something. Is performance one thing that struck me as one of the potential bottlenecks?

thanks

+6
source share
8 answers

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

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

+15
source share
+8
source share

Now I shamelessly copy my edited answer for a very similar question asked here today :

I would like to point out that while arrays are covariance, there are no common lists. For example, an array of type MyChildClass[] can be easily discarded to MyParentClass[] , while List<MyChildClass> cannot be executed before List<MyParentClass> , at least not directly.

If you need covariance, use arrays, use the LINQ Cast () method, or some other way to cast each item individually.

+3
source share

When you use a fixed number of a certain type of object, it makes no sense to take a small performance hit associated with the List class, since you will not use any of its functions.

+2
source share

Another reason for using regular arrays is when you are working with a platform call (running unmanaged c / C ++ code), but this is limited to a very small set of applications

Also, as a note: if the size of the array is known, but you still want to use a generic list, remember to pass the size to the constructor's capacity parameter.

eg:

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

Otherwise, the list will start with a very small capacity and several times will need to allocate new pieces of memory, instead of immediately allocating the necessary space.

+1
source share

Many data structures of a higher level (including List) use internal arrays, this is the only way to save the raw sequence in memory - therefore, the array must exist in a programming language, otherwise you would not have more strict data structures.

My opinion is that if you do not need (for example, because you use a component that requires it), you should never use arrays - especially, do not write code that returns arrays, accept arrays as parameters, so you do not force others use arrays too.

The only exception (in my opinion) is a rare case when you need a collection of a known fixed size as a local variable, than just wasteful use of something else.

0
source share

As far as we know, arrays are a primitive data structure, if you need to insert or delete elements in the middle position, and also sort them, usually in obsolete languages ​​it takes a programmer to do this, instead, when you work with a List object, such as DotNet Generics, all of these operations are already done for you.

More, arrays are very limited, only the same data type elements, etc.

At the present time, I see no advantages when using arrays.

Sincerely.

0
source share

If you want to cache a fixed amount of data, use arrays. Otherwise, use shared lists ... they are simply easier to use, provide more flexibility and are typed (no boxing / unpacking is necessary) ... simple. In addition, an array is a fixed length, while lists can be variable and change whenever you want. The reasons can go on and on why you should use a common list compared to an array. Arrays are just pain in the ass. And, frankly, performance using a list compared to an array may be discussed to the nth degree.

0
source share

All Articles