660
MyClass[] array;
List<MyClass> list;

What are the scenarios when one is preferable over the other? And why?

nawfal
  • 62,042
  • 48
  • 302
  • 339
Frederick The Fool
  • 31,355
  • 20
  • 78
  • 112
  • 12
    Arrays are rather obsolete, as seen in a [popular discussion](http://stackoverflow.com/questions/392397/arrays-whats-the-point) here. Also [pointed out here](http://damienkatz.net/2008/12/arrays_whats_the_point_good_qu.html), and by our host [in the blog](http://blog.stackoverflow.com/2008/12/). – gimel Jan 12 '09 at 08:14
  • 11
    If I'm not mistaken the List<> has an array as internal structure. Whenever the internal array is filled it simply copy the content to an array that is double the size (or some other constant times the current size). http://en.wikipedia.org/wiki/Dynamic_array – Ykok Sep 12 '13 at 12:15
  • Ykok: What you say seems about right, I found the source code of [List<> here](http://reflector.webtropy.com/default.aspx/Net/Net/3@5@50727@3053/DEVDIV/depot/DevDiv/releases/whidbey/netfxsp/ndp/clr/src/BCL/System/Collections/Generic/List@cs/2/List@cs). – Carol Nov 22 '14 at 06:20
  • 27
    @gimel Arguing that arrays are obsolete is perhaps a bit bold – awdz9nld Nov 25 '15 at 15:08

15 Answers15

646

It is rare, in reality, that you would want to use an array. Definitely use a List<T> any time you want to add/remove data, since resizing arrays is expensive. If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful.

List<T> offers a lot more functionality than an array (although LINQ evens it up a bit), and is almost always the right choice. Except for params arguments, of course. ;-p

As a counter - List<T> is one-dimensional; where-as you have have rectangular (etc) arrays like int[,] or string[,,] - but there are other ways of modelling such data (if you need) in an object model.

See also:

That said, I make a lot of use of arrays in my protobuf-net project; entirely for performance:

  • it does a lot of bit-shifting, so a byte[] is pretty much essential for encoding;
  • I use a local rolling byte[] buffer which I fill before sending down to the underlying stream (and v.v.); quicker than BufferedStream etc;
  • it internally uses an array-based model of objects (Foo[] rather than List<Foo>), since the size is fixed once built, and needs to be very fast.

But this is definitely an exception; for general line-of-business processing, a List<T> wins every time.

Community
  • 1
  • 1
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 11
    The argument about resizing is totally valid. However people prefer Lists even when no resizing is needed. For this latter case, is there a solid, logical argument or is it nothing more than "arrays are out of fashion"? – Frederick The Fool Jan 12 '09 at 08:25
  • @Frederick: hard to answer without hearing the "why". Actually, in many cases I'd prefer `IList` or `IEnumerable` - it lets the caller decide what they want to use (`T[]` or `List` etc). LINQ largely balances out a lot of the extra `List` functions (over `IList`). – Marc Gravell Jan 12 '09 at 08:28
  • 1
    Actually, I'm working with a legacy, C# 1.0 codebase. Some of the functions there take and return arrays. No resizing or anything is happening.... – Frederick The Fool Jan 12 '09 at 08:37
  • 1
    ...Now, in my wrapper functions, I'm inclined to use List's instead of arrays. But I have no solid reasoning to justify this. Just a warm fuzzy feeling. I need a good argument for my decision. – Frederick The Fool Jan 12 '09 at 08:38
  • Well, things are slightly different without generics... Boxing and casting aren't the *worst* things in the world, but it adds up. I'd say that arrays have a *bit* more place in .NET 1.x than now, but there is still ArrayList etc if you can live with the box/cast/no-compile-time-checking pain. – Marc Gravell Jan 12 '09 at 08:40
  • @Frederick - seriously: consider IList (if you need add/remove/indexer) or IEnumerable (if you just need `foreach`). – Marc Gravell Jan 12 '09 at 08:41
  • I don't fully understand this: does working with an array need boxing/unboxing? Even if the type contained in the array is a ref type? And is casting needed too? But isn't an array strongly typed? PS: I'm just curious if there's just some dogma against arrays. PPS: Yet to read Jon Skeet's link. – Frederick The Fool Jan 12 '09 at 08:49
  • Yeah. I appreciate and agree with using IList and IEnumerable. They make your class user less constrained in terms of what to supply to and expect back from your class. – Frederick The Fool Jan 12 '09 at 08:51
  • @Frederick - my boxing/cast comments were directed at ArrayList etc, since they are the closest option to List in .NET 1.x – Marc Gravell Jan 12 '09 at 08:55
  • 12
    "Definitely use a List any time you want to add/remove data, since resizing arrays is expensive." List uses an array internally. Were you thinking of LinkedList? – dan-gph Mar 09 '10 at 06:49
  • 3
    @Dangph: A List keeps the array bigger than it needs to be, so not every add/remove operation needs to resize the array, since there's still free space at the end of the array most of the time. – Lucas Werkmeister Oct 17 '13 at 15:04
  • 1
    @lucas, but what if you remove an item from the middle of the array? All the elements above it will have to be shuffled along to fill up the hole. – dan-gph Oct 18 '13 at 03:17
  • `Except for params arguments, of course ;-p` Why except for params arguments? Could you explain a bit? – Gaui Nov 04 '13 at 11:48
  • @Gaui because `params` **only** exists for vectors; so this is one of the places where it is more natural to use a naked array. Of course, personally I'm very anti-invisible-allocation, so I try to avoid `params` in the first place. – Marc Gravell Nov 04 '13 at 11:53
  • You wrote "there are other ways of modelling such [n-dimensional] data (if you need) in an object model." Well I do need, since the application I work makes heavy use of n-dimensional data, and I am happily using `double[,]`, `Point3D[,]`, and the like. Do you have any resource to point regarding appropriate object models for that use, which are better than plain arrays? My arrays are usually dense and do not resize or reshape. Thanks! – heltonbiker Jun 11 '14 at 19:51
  • 20
    More features == more complex == not good, unless you need those features. This answer basically lists reasons why array's are better, yet draws the opposite conclusion. – Eamon Nerbonne Jun 20 '14 at 09:35
  • @lucas.werkmeister Yes. There's nothing stopping you from doubling the array size when it fills yourself either so the expense of resizing an array is irrelevant. – Casey Jul 17 '14 at 21:17
  • @EamonNerbonne I don't really agree with that. Most of the "features" you're talking about (like implementing dynamic arrays) are not like, newfangled junk you're hardly ever going to use. – Casey Jul 17 '14 at 21:19
  • @emodendroket It doesn't matter whether they're newfangled, it's extra complexity you just don't need. Are you actually using those features? No? Don't use a `List<>`. Since datastructures that change over time make code difficult to reason about, I'd actively recommend you *don't* use `List<>.Add` unless you've given it careful consideration; array's simply remove one more source of bugs in code that doesn't need dynamic resizing (i.e. most code). – Eamon Nerbonne Jul 18 '14 at 11:48
  • 14
    @EamonNerbonne if you're not using those features, I can pretty much guarantee that they aren't going to hurt you... but: the number of collections that never need mutation is **much** smaller, in my experience, than those that are mutated – Marc Gravell Jul 18 '14 at 11:52
  • 9
    @MarcGravell: that depends on your coding style. In my experience virtually no collections are ever mutated. That is; collections are retrieved from the database or constructed from some source, but further processing is always done by recreating a new collection (e.g. map/filter etc). Even where conceptual mutation is necessary, it tends to be simplest to just generate a new collection. I only ever mutate a collection as a performance optimization, and such optimizations tend to be highly local and not expose the mutation to API consumers. – Eamon Nerbonne Jul 18 '14 at 12:38
  • 1
    @MarcGravell: For some context, non-mutating collections is clearly what LINQ is best at, and that's not an uncommon API nowadays. It's both odd to "mutate" the result of a linq query (why didn't you select+project correctly in the first place?), and it's conceptually tricky to mutate the source of a linq query due to the mixture of eager and lazy evaluation. If you're building a database-backed website, I'd be curious if there's any (common) use case at all for mutable collections - outside the database itself, of course. – Eamon Nerbonne Jul 18 '14 at 12:47
  • 2
    @EamonNerbonne I couldn't disagree more that "most code" does not need array resizing and there are lots of other things you have to worry about going wrong if you're working with arrays. – Casey Jul 18 '14 at 13:48
  • 1
    @emodendroket: Some code deals with collections by manually looping, processing datastructures as it goes. This code likes `foreach` and `List<>.Add`. Other code deals with collections as values, and computes new collections from old. Such code likes LINQ, and has little use for mutable collections. This answer implicitly prefers loops+mutation over functional constructs, and I don't think such a preference should be implicitly stated as fact the way it is. Most code I see tends towards the functional - computing new collections (or iterators) from existing, unchanged data. – Eamon Nerbonne Jul 18 '14 at 14:52
  • @MarcGravell: `List<>` mutability hurts in practice precisely because some code conventionally expects changes to performed by mutation. Avoiding the `Add` method immediately avoids the #1 source of accidental misuse, and from experience in a large project, I can say that such bugs are nasty because (just like null-deref bugs) the code that crashes or computes the wrong value is often far removed from the source of the error (the code that changed an internal datastructure that was presumed to be unchanging). TL;DR: If you're going the LINQ route, what's the use-case for `List<>`? – Eamon Nerbonne Jul 18 '14 at 14:57
  • 1
    @EamonNerbonne If you want immutable collections, you should actually use an immutable collection. Arrays are mutable; they're just fixed in length. – Casey Jul 18 '14 at 16:51
  • 2
    @EamonNerbonne Arrays are the best choice in the rather infrequent circumstance that you *need* a mutable collection, but one that is fixed in length. That isn't functional programming at all. – Casey Jul 18 '14 at 16:52
  • 1
    @emodendroket: Arrays aren't perfect, but they're better than `List<>` if you don't need mutability: they're faster, use less memory, and avoid the most common mutable operation. Unfortunately, .NET does not provide a comparable immutable array (there's a nuget package for System.Collections.Immutable, but the array implementation is still prerelease only), and even once it does, not everyone can update to the latest .NET immediately and the immutability wrapper will have some overhead. All in all: right now, Array is good enough (and certainly better than `List<>` non-mutating use-cases. – Eamon Nerbonne Jul 21 '14 at 12:39
  • 1
    @emodendroket: think of it this way: precisely because `List<>` is so appropriate for mutating collections, it's inappropriate for non-mutating collections because the type conveys the wrong message. – Eamon Nerbonne Jul 21 '14 at 12:40
  • @EamonNerbonne Well, using `ReadOnlyCollection` would send that message even more strongly. – Casey Jul 21 '14 at 20:49
  • 1
    @emodendroket: `ReadOnlyCollection` a lot more verbose, and [horrifically slow](http://eamon.nerbonne.org/2009/02/net-numeric-performance-disappointing.html). For a public API - sure. But most code isn't in the public API, and coding conventions that state "don't mess with array contents after construction" work fine. Enforces immutability isn't natural or easy in C#, and I don't think the additional mental and runtime overhead is worth it, especially when simple conventions are almost just as good, and much simpler and faster. – Eamon Nerbonne Jul 22 '14 at 07:08
  • why do you say "List is one-dimensional; where-as you have have rectangular (etc) arrays" > So why suggest that List is 1D when it's not anymore or less 1D than arrays? – barlop Sep 23 '16 at 05:02
  • 2
    @barlop k; `int[,]` is explicitly a 2D array. `int[,,]` is explicitly a 3d array. `int[][]` however is not a "2d array" - it is a 1D array of 1D arrays, aka a jagged array. Likewise `List>` is not a 2D list: it is a list of lists. The point is that in the case of `int[,]` there is **one array**, so we can describe the properties of that 1 array. In the case of `List>` there are n+1 lists; one is a 1D list of lists, when are 1D lists of ints. Yes, you can use a `List>` to express 2D data, but that isn't the same thing. Semantics matter. – Marc Gravell Sep 23 '16 at 06:53
  • @MarcGravell What about switching between Lists and Arrays when the case requires. i.e, once the list is size-fixed, switch to array and perform stuff on the array, and maybe if we need to extensively add/remove again switch back again to lists (using LINQ) – tfrascaroli Aug 29 '17 at 12:11
  • @tfrascaroli you can't simply switch between arrays and lists without paying allocation+copy costs which would make any such approach prohibitively expensive – Marc Gravell Aug 29 '17 at 20:11
  • @MarcGravell yeah, I figured that. The thing is, if list is cheaper to add/remove elements, and then an array is cheaper to read and use, maybe a conversion right after we're done adding wouldn' be that bad. But I agree that the approach looks to be a complete waste of resources. I don't know how the conversions in LINQ are implemented internally though... – tfrascaroli Aug 29 '17 at 20:39
  • Arrays for variadic `params` or to guarantee fixed size. List to deal with resize. – solstice333 Jan 31 '19 at 11:59
  • Also, some API's just naturally return Array like for example `Regex.split` – solstice333 Jan 31 '19 at 12:14
  • Old post, but this really should mention something about memory usage. Lists containing complex objects take up significantly more space in memory. – Tony Cheetham May 13 '20 at 11:05
  • @TonyCheetham this can only be true if you are taking about lists/arrays of value types as opposed to reference types – Cesar Jul 10 '20 at 11:59
127

Really just answering to add a link which I'm surprised hasn't been mentioned yet: Eric's Lippert's blog entry on "Arrays considered somewhat harmful."

You can judge from the title that it's suggesting using collections wherever practical - but as Marc rightly points out, there are plenty of places where an array really is the only practical solution.

Brian
  • 24,434
  • 16
  • 74
  • 162
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
24

Notwithstanding the other answers recommending List<T>, you'll want to use arrays when handling:

  • image bitmap data
  • other low-level data-structures (i.e. network protocols)
Alnitak
  • 313,276
  • 69
  • 379
  • 466
  • 1
    Why for network protocols? Wouldn't you rather use custom structures here and give them an special serializer or an explicit memory layout? Furthermore, what speaks against using a `List` here rather than a byte array? – Konrad Rudolph Jan 12 '09 at 08:28
  • 9
    @Konrad - well, for starters, Stream.Read and Stream.Write work with byte[], as does Encoding etc... – Marc Gravell Jan 12 '09 at 08:37
13

Unless you are really concerned with performance, and by that I mean, "Why are you using .Net instead of C++?" you should stick with List<>. It's easier to maintain and does all the dirty work of resizing an array behind the scenes for you. (If necessary, List<> is pretty smart about choosing array sizes so it doesn't need to usually.)

Spencer Ruport
  • 34,215
  • 11
  • 81
  • 141
  • 18
    "Why are you using .Net instead of C++?" XNA – Bengt Jun 12 '11 at 04:54
  • To elaborate on @Bengt comment, performance is not .NET's priority and this is by all means fair. But some bright minds thought of using .NET with game engines. In fact most game engines use C# nowadays. In this regard Unity 3D is the paradigm of "When a game engine was not meant for AAA". – mireazma Oct 07 '20 at 05:25
  • @mireazma That hasn't been a valid statement (wrt Unity) for a long time. We're getting C++ performance out of C# via HPC# and Burst. A lot of the engine internals are being migrated to C#. Even if you're talking about game script in a non-DOTS project, IL2CPP does a very, very good job of producing performant code. – 3Dave Feb 19 '21 at 18:45
  • @3Dave Let's not start a polemic on this. "IL2CPP does a very, very good job of producing performant code". Indeed it does, to the extents of its reach. Contrary to https://www.jacksondunstan.com/articles/3001 (in 2015) I think that IL2CPP compared to mono does a very good job performance wise. But compared to mono. Compared to native C++ it's nonsense. I did an arithmetic comparative test in Unity with il2cpp in a _released_ exe with all optimizations vs C++ and the times were 18745, 18487ms and more times around there in Unity 2020 vs 189ms in C++ (at times ~36ms). Do a test yourself. – mireazma Feb 21 '21 at 15:06
  • @mireazma My comment was intented to be more focused on Burst and HPC#, where we are seeing performance as good (and in some cases better) than equivalent native code. Your benchmarking numbers are interesting, though. I'll look into that. Cheers. – 3Dave Feb 21 '21 at 19:38
8

Arrays should be used in preference to List when the immutability of the collection itself is part of the contract between the client & provider code (not necessarily immutability of the items within the collection) AND when IEnumerable is not suitable.

For example,

var str = "This is a string";
var strChars = str.ToCharArray();  // returns array

It is clear that modification of "strChars" will not mutate the original "str" object, irrespective implementation-level knowledge of "str"'s underlying type.

But suppose that

var str = "This is a string";
var strChars = str.ToCharList();  // returns List<char>
strChars.Insert(0, 'X');

In this case, it's not clear from that code-snippet alone if the insert method will or will not mutate the original "str" object. It requires implementation level knowledge of String to make that determination, which breaks Design by Contract approach. In the case of String, it's not a big deal, but it can be a big deal in almost every other case. Setting the List to read-only does help but results in run-time errors, not compile-time.

Herman Schoenfeld
  • 7,500
  • 3
  • 33
  • 48
  • I'm relatively new to C# but it's not clear to me why returning a list would suggest mutability of the original data in a way that returning an array wouldn't. I would have though that a method whose name starts with `To` is going to create an object that has no ability to modify the original instance, as opposed to `strChars as char[]` which if valid would suggest you're now able to modify the original object. – Tim MB Nov 29 '19 at 11:20
  • @TimMB There's the immutability of the collection (cannot add or remote items) and immutability of the items in the collection. I was referring to the latter, whereas you're probably conflating the two. Returning an array assures the client it cannot add/remove items. If it does, it re-allocates the array and is assured it will not affect original. Returning a list, no such assurances are made and original could be affected (depends on implementation). Changing items within the collection (whether array or list) could affect the original, if the item's type is not a struct. – Herman Schoenfeld Jan 10 '20 at 07:58
  • Thank you for the clarification. I'm still confused (probably because I come from the C++ world). If `str` internally uses an array and `ToCharArray` returns a reference to this array then the client can mutate `str` by changing the elements of that array, even if the size remains fixed. Yet you write 'It is clear that modification of "strChars" will not mutate the original "str" object'. What am I missing here? From what I can see, in either case the client may have access to the internal representation and, regardless of the type, this would allow mutation of some kind. – Tim MB Jan 12 '20 at 14:06
4

If I know exactly how many elements I'm going to need, say I need 5 elements and only ever 5 elements then I use an array. Otherwise I just use a List<T>.

smack0007
  • 10,165
  • 7
  • 38
  • 46
3

Most of the times, using a List would suffice. A List uses an internal array to handle its data, and automatically resizes the array when adding more elements to the List than its current capacity, which makes it more easy to use than an array, where you need to know the capacity beforehand.

See http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx#datastructures20_1_topic5 for more information about Lists in C# or just decompile System.Collections.Generic.List<T>.

If you need multidimensional data (for example using a matrix or in graphics programming), you would probably go with an array instead.

As always, if memory or performance is an issue, measure it! Otherwise you could be making false assumptions about the code.

Sune Rievers
  • 2,386
  • 3
  • 23
  • 29
  • 1
    Hi, could you explain why "A list's lookup time would be O(n)" is true? As far as I know List uses array behind the scenes. – dragonfly Aug 07 '12 at 06:54
  • 1
    @dragonfly you're totally right. [Source](http://msdn.microsoft.com/en-us/library/0ebtbkkc). At the time, I assumed that the implementation used pointers, but I've since learned otherwise. From the link above: 'Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.' – Sune Rievers Aug 07 '12 at 08:30
2

Arrays Vs. Lists is a classic maintainability vs. performance problem. The rule of thumb that nearly all developers follow is that you should shoot for both, but when they come in to conflict, choose maintainability over performance. The exception to that rule is when performance has already proven to be an issue. If you carry this principle in to Arrays Vs. Lists, then what you get is this:

Use strongly typed lists until you hit performance problems. If you hit a performance problem, make a decision as to whether dropping out to arrays will benefit your solution with performance more than it will be a detriment to your solution in terms of maintenance.

Christian Findlay
  • 4,791
  • 1
  • 33
  • 74
1

Another situation not yet mentioned is when one will have a large number of items, each of which consists of a fixed bunch of related-but-independent variables stuck together (e.g. the coordinates of a point, or the vertices of a 3d triangle). An array of exposed-field structures will allow the its elements to be efficiently modified "in place"--something which is not possible with any other collection type. Because an array of structures holds its elements consecutively in RAM, sequential accesses to array elements can be very fast. In situations where code will need to make many sequential passes through an array, an array of structures may outperform an array or other collection of class object references by a factor of 2:1; further, the ability to update elements in place may allow an array of structures to outperform any other kind of collection of structures.

Although arrays are not resizable, it is not difficult to have code store an array reference along with the number of elements that are in use, and replace the array with a larger one as required. Alternatively, one could easily write code for a type which behaved much like a List<T> but exposed its backing store, thus allowing one to say either MyPoints.Add(nextPoint); or MyPoints.Items[23].X += 5;. Note that the latter would not necessarily throw an exception if code tried to access beyond the end of the list, but usage would otherwise be conceptually quite similar to List<T>.

supercat
  • 69,493
  • 7
  • 143
  • 184
  • What you described is a List<>. There's an indexer so you can access the underlying array directly, and the List<> will maintain the size for you. – Carl Sep 15 '17 at 07:58
  • @Carl: Given e.g. `Point[] arr;`, it's possible for code to say, e.g. `arr[3].x+=q;`. Using e.g. `List list`, it would be necessary to instead say `Point temp=list[3]; temp.x+=q; list[3]=temp;`. It would be helpful if `List` had a method `Update(int index, ActionByRefRef proc, ref TP params)`. and compilers could turn `list[3].x+=q;` into `{list.Update(3, (ref int value, ref int param)=>value+=param, ref q);` but no such feature exists. – supercat Sep 15 '17 at 15:09
  • Good news. It works. `list[0].X += 3;` will add 3 to the X property of the first element of the list. And `list` is a `List` and `Point` is a class with X and Y properties – Carl Sep 17 '17 at 01:53
1

Lists in .NET are wrappers over arrays, and use an array internally. The time complexity of operations on lists is the same as would be with arrays, however there is a little more overhead with all the added functionality / ease of use of lists (such as automatic resizing and the methods that come with the list class). Pretty much, I would recommend using lists in all cases unless there is a compelling reason not to do so, such as if you need to write extremely optimized code, or are working with other code that is built around arrays.

iliketocode
  • 6,652
  • 4
  • 41
  • 57
0

Rather than going through a comparison of the features of each data type, I think the most pragmatic answer is "the differences probably aren't that important for what you need to accomplish, especially since they both implement IEnumerable, so follow popular convention and use a List until you have a reason not to, at which point you probably will have your reason for using an array over a List."

Most of the time in managed code you're going to want to favor collections being as easy to work with as possible over worrying about micro-optimizations.

moarboilerplate
  • 1,573
  • 7
  • 21
0

Since no one mention: In C#, an array is a list. MyClass[] and List<MyClass> both implement IList<MyClass>. (e.g. void Foo(IList<int> foo) can be called like Foo(new[] { 1, 2, 3 }) or Foo(new List<int> { 1, 2, 3 }) )

So, if you are writing a method that accepts a List<MyClass> as an argument, but uses only subset of features, you may want to declare as IList<MyClass> instead for callers' convenience.

Details:

snipsnipsnip
  • 1,678
  • 29
  • 25
  • *"In C#, an array is a list"* That's not true; an array is not a `List`, it only implements the `IList` interface. – Rufus L Nov 10 '19 at 04:28
0

They may be unpopular, but I am a fan of Arrays in game projects. - Iteration speed can be important in some cases, foreach on an Array has significantly less overhead if you are not doing much per element - Adding and removing is not that hard with helper functions - Its slower, but in cases where you only build it once it may not matter - In most cases, less extra memory is wasted (only really significant with Arrays of structs) - Slightly less garbage and pointers and pointer chasing

That being said, I use List far more often than Arrays in practice, but they each have their place.

It would be nice if List where a built in type so that they could optimize out the wrapper and enumeration overhead.

0

Populating a list is easier than an array. For arrays, you need to know the exact length of data, but for lists, data size can be any. And, you can convert a list into an array.

List<URLDTO> urls = new List<URLDTO>();

urls.Add(new URLDTO() {
    key = "wiki",
    url = "https://...",
});

urls.Add(new URLDTO()
{
    key = "url",
    url = "http://...",
});

urls.Add(new URLDTO()
{
    key = "dir",
    url = "https://...",
});

// convert a list into an array: URLDTO[]
return urls.ToArray();
Bimal Poudel
  • 1,072
  • 2
  • 14
  • 37
-2

It completely depends on the contexts in which the data structure is needed. For example, if you are creating items to be used by other functions or services using List is the perfect way to accomplish it.

Now if you have a list of items and you just want to display them, say on a web page array is the container you need to use.

sajidnizami
  • 487
  • 5
  • 15
  • 1
    If you have a list of items and you just want to display them, then what is wrong with just using the list you already have? What would an array offer here? – Marc Gravell Jan 12 '09 at 08:18
  • 1
    And for "creating items to be used by other functions or services", actually, I'd prefer an iterator block with `IEnumerable` - then I can stream objects rather than buffer them. – Marc Gravell Jan 12 '09 at 08:47