1

I am working on a project and I noticed that the person used a list for a piece of code. Had it been me, I would have used an array simply out of personal preference because that is what I know. Looking at the code below, what benefit would choosing the list have over the array?

List<string> errors = new List<string>();

if (ddDirector.SelectedItem.Value == "") 
   errors.Add("You must select an item in the Director list.");

if (errors.Count > 0)
{
   ErrorList.InnerHtml = "Please correct the following issues below:<br/><ul>";
   foreach (string e in errors)
   {
      ErrorList.InnerHtml += String.Format("<li>{0}</li>", e);
   }
   ErrorList.InnerHtml += "</ul>";
}
return (errors.Count()==0);
Brad
  • 359
  • 5
  • 21
  • 3
    A List has methods to easily add and subtract elements at given positions or from the beginning or end as well as a slew of other methods. https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx – ProgrammingDude Jan 19 '16 at 13:15
  • 1
    Honestly, in my experience, people use `List` because they're lazy and it's easy to use. It's not always the best choice from a performance perspective though. – CodingGorilla Jan 19 '16 at 13:16
  • 1
    If you are not sure about the number of items, then go for list. – Mohammad Arshad Alam Jan 19 '16 at 13:17
  • 4
    @CodingGorilla to be fair, you qualified it with "in my experience", but I still feel this is a gross generalisation. – David Pilkington Jan 19 '16 at 13:17
  • There's a lot of info on this [Here](http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which). – Gilgamesh Jan 19 '16 at 13:18
  • 1
    Eric Lippert's analysis is useful also: https://blogs.msdn.microsoft.com/ericlippert/2008/09/22/arrays-considered-somewhat-harmful/ – Pieter Geerkens Jan 19 '16 at 13:20
  • An aside: Rather than `errors.Count()==0` you can use the [`Any`](https://msdn.microsoft.com/en-gb/library/bb337697(v=vs.90).aspx) extension method: `!errors.Any()` – Richard Ev Jan 19 '16 at 13:21
  • 1
    By the way, if you're writing methods that accept or return lists/arrays you should look for opportunities to use an interface type such as `IList`, `IReadOnlyList`or `ICollection` etc. Both arrays and lists implement these interfaces so you can achieve a higher level of abstraction by doing so, the concrete type of the collection then becomes an irrelevance. – TheInnerLight Jan 19 '16 at 13:23

2 Answers2

5

For an array, you need to know the size of the collection before it is initialised.

This is not needed for a List so it can expand as needed.

As an aside, a List has a Count property that you can use instead of the Count() extension method.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • 1
    Arrays have Length, you don't have to use Count() – galenus Jan 19 '16 at 13:17
  • 1
    @galenus I know, I was referring to the code snippet. Saying that he didnt have to use the extension method there. I wasnt including it as a reason to use List over an array. – David Pilkington Jan 19 '16 at 13:18
  • 1
    Actually you can use `Enumerable.Count` on arrays and lists. There is no performance difference because the method first checks if the collection can be casted to `ICollection` which is the case for lists and arrays. Then the property is used. – Tim Schmelter Jan 19 '16 at 13:20
  • @David sorry, didn't get your intention – galenus Jan 19 '16 at 13:20
2

An array have a fixed size. If you do not fill it completely you'll also have to have a variable which stores the number of used slots in the array.

A list usually wraps an array, but takes care of allocating a larger array (if required) and keeping track of the used size.

jgauffin
  • 99,844
  • 45
  • 235
  • 372