Biki
Biki

Reputation: 2588

Where to use string [] vs list <string> in C#

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?

Upvotes: 43

Views: 27963

Answers (6)

Prem
Prem

Reputation: 432

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.

Upvotes: 2

Kimtho6
Kimtho6

Reputation: 6184

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;

Upvotes: 2

Peter Lillevold
Peter Lillevold

Reputation: 33920

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>.

Upvotes: 9

CodingBarfield
CodingBarfield

Reputation: 3398

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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>.

Upvotes: 64

Matt
Matt

Reputation: 1422

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.

Upvotes: 4

Related Questions