43

String[] is light weight compared to list<string>. So if I don't have any need to manipulate my collection, should I use string[] or is it always advisable to go for list<string>?

In case of list<string>, do we need to perform null check or not required?

2
  • 1
    You need do null check on any reference type, including the list. Commented Jan 18, 2011 at 13:53
  • @ J.W. 1 point up: For any reference type Null check is mandatory. Commented Jan 18, 2011 at 14:11

6 Answers 6

65

Use string[] when you need to work with static arrays: you don't need to add and remove elements -> only access elements by index. If you need to modify the collection use List<string>. And if you intend to only loop through the contents and never access by index use IEnumerable<string>.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I know this is an 'old' post, but can you give a reason why to use the string[] in that situation (or a link)? I found nothing on MSDN about the pro's of String[] compared to List<string>. Is it better for static arrays simply because it's more lightweight?
@DaveRook You are correct: List<> has a lot of overhead with the functions e.t.c that it can provide. String[] is a simple, bare array with no extra 'functionality'. If you want speed, and you don't need to dynamically alter the list, use string[].
9

If the collection should not be modified, use string[] or even better, IEnumerable<string>. This indicates that the collection of strings should be treated as a read-only collection of strings.

Using IEnumerable<string> in your API also opens up for the underlying implementation to be changed without breaking client code. You can easily use a string array as the underlying implementation and expose it as IEnumerable<string>. If the implementation at a later stage is better suited using a list or other structure, you can change it as long as it supports IEnumerable<string>.

Comments

4

I'd say you've summed it up well yourself.

If the size of your list won't change, and you don't need any of the advanced List functions like sorting, then String[] is preferable because as you say it's lightweight.

But consider potential future requirements - is it possible that you might one day want to use List for something? If so, consider using List now.

You need to check for null, both in String[] and also List. Both types can have a null value.

3 Comments

Do the simplest thing. Considering "what might possibly be the requirement one day" is dangerous, reminds me of Big-Design-Upfront :)
@Peter While I do agree, I think this the way of thinking of an enterprise app. developer, whereas I come from a component writing background where you have to consider everything that users "might" want to do with your component. Like someone else has suggested, it depends very heavily on the context.
well, I've seen code where List is sprinkled all over the place just because it is convenient. With the result of lock-in on implementation. If a component, being enterprisy or not, delivers a list of data, the contract and the purpose of that component should dictate what data type should be returned. If the client needs to modify the collection, it is only a matter of newing up a List.
2

I would say it depends what you're trying to accomplish. Generally, however, my opinion is that you have access to a great framework that does a lot of hard work for you so use it (ie. use List<> instead of array).

Have a look at the members on offer to you by a class like List<> and you'll see what I mean: in addition to not having to worry as much about array capacity and index out of bounds exceptions, List and other ICollection/IList classes give you methods like Add, Remove, Clear, Insert, Find, etc that are infinitely helpful. I also believe

myList.Add (myWidg);

is a lot nicer to read and maintain than

myArr [i] = myWidg;

Comments

2

I would definitely vote for List. Apart from various member functions that a list supports, it provides 'no element' concept. There can be a list which have no elements but there cannot be an array with no elements. So, if we adhere to best practices of not returning null from a function, then we can safely check for the count of the element without doing a null check. In case of array, we have to check the null. Moreover, I seldom use a loop to search an element, either in array or list. LINQ just makes it neat and we can use it with List not array. Array has to be converted to list to make use of LINQ.

Comments

1

This really really depends on the situation. Anything really performance related should probably be done with arrays. Anything else would go with lists.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.