开发者

Is List good than array in C#? [duplicate]

开发者 https://www.devze.com 2023-01-07 11:06 出处:网络
This question already has answers here: 开发者_高级运维 Closed 12 years ago. Possible Duplicate: Array versus List<T>: When to use which?
This question already has answers here: 开发者_高级运维 Closed 12 years ago.

Possible Duplicate:

Array versus List<T>: When to use which?

i.e

// ex1
IList<string> list

// ex2
string[] str

the List can init without length, but when I need read it, is it good as array?

and Can I make all string[] instead of IList<string>?

are there anything can array make done but List?


define good!!

is there any performance issue?
could this performance issue be solved by using array over list? (depends on your implementation)
do you want to target all .net-frameworks? do you target any explicit .net-framework which does not support the generic list/dictionary?
do you need interop with win32? and so on, and so on ...

why struggle with this question, just use the List<T> or Dictionary<TKey, TValue> until you get any (performance) issues.
it's much more handy to use, gives tons of properties/methods/extension-methods and microsoft does a good job in optimizing its own datastructures!

just to mention: if you worry about interop between this 2 structures, you might use System.Linq-namespace to gain:

list.ToArray();
array.ToList();


In most cases List<> will be better. It's clearer, it makes it easier to use, loop, has many useful properties and methods and it doesn't need a contiguous memory block that an array needs. It grows / shrinks when you want and your code will simply become more readable.

In terms of performance, in many cases List<> may outperform an array, especially if you need to grow / shrink, but only measuring it will really show and, really, this type of performance optimization is hardly needed these days.

Use arrays when you must pass big chunks of memory around (array of bytes) or need to do interoperability (i.e., no Win32 API will understand List). Use List.ToArray() for these cases to have the best of both worlds.

In cases where you must lookup things in your array or list, a Dictionary is likely the more suitable candidate and it is blazingly fast in all but a few scenarios.


Know your requirements when you choose

If you have a fixed length amount of data and you know specific byte offsets of the data you want to manipulate, use an array.

For instance. If you were reading a network packet. You know the length of the data you want to read. You know the offsets of the data you want to access. Which means, an array would be the optimal choice. To use a linked list, you'd have to crawl the list element by element to access the data you want, with an array you set the offset and use a Buffer.BlockCopy() Array.Copy() to pull the data you need in an optimal fashion.

There are few cases where an array is the optimal choice. Lists are much easier to use and well supported these days.

I look at it like this, if the data buffer size is fixed and the offsets are predictable/calculable use an array. Otherwise, use a list.

The list of cases where arrays are optimal usually include:

  • network protocols
  • data protocols
  • binary data files
  • statically defined data structures


What you've got in your example is not a List<T> it is an IList<T>, which is just an interface, that a lot of classes uses - List<T> being one of them.

I use List<T> over arrays all the time, and I think it's great, although it is perhaps better to think of it as replacing ArrayList, than replacing arrays...

You can always switch between them, at will, myArray.ToList(), myList.ToArray(), of course if you are going to do that a lot, there'll be minor performance considerations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号