0

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?

Community
  • 1
  • 1
user386207
  • 17
  • 3
  • 1
    Please be a bit more specific about *good* and *good as array*. Usage? Speed? Algorithms? ASP.NET pages? Each has its own best answer. – Abel Jul 12 '10 at 10:11
  • @Oliver: as the querist doesn't elaborate, ... but i think you're right! –  Jul 12 '10 at 10:20

4 Answers4

2

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();
  • +1 for warning of premature optimization; -1 for assuming that the person asking the question does not have a good reason for doing so. – Heinzi Jul 12 '10 at 10:02
  • this is **not** answer to the question. such information should go to comments. – Andrey Jul 12 '10 at 10:04
  • 2
    his question is vague as hell! what is `good`? so i suppose it's not a real question about performance/... just about working with this new class. –  Jul 12 '10 at 10:04
  • @Andreas Niedermair his question is vague but your answer is vague also, you didn't give any reasons why it is `good`. – Andrey Jul 12 '10 at 10:12
  • nope ... it's mainly `go on, try new things, until you experience any problems`. my questions give implicit answers as well! –  Jul 12 '10 at 10:14
  • and since `good` is not defined, there can't be any valid answer to his question... go on, blame the querist! –  Jul 12 '10 at 10:16
1

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.

Abel
  • 52,738
  • 19
  • 137
  • 227
1

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
Evan Plaice
  • 13,310
  • 4
  • 70
  • 94
  • If anybody has any better use cases speak up and I'll add them. I use arrays (and/or stream readers) heavily in networking code and on a few rare cases where I had to parse binary files but that's it. I'd like to hear some other cases where arrays are useful. – Evan Plaice Jul 12 '10 at 10:48
0

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.

David Hedlund
  • 121,697
  • 28
  • 196
  • 213