Both Lists and Arrays in C# are collections* that can hold simple types, objects and structs. We will start by going through the basics of arrays and lists.
*) By collection, we do not mean that both List and Array should be in the system.collection namespace (only List is in that namespace), but that they both hold "collections of items".
Arrays
Arrays have a fixed length, you define the length of the array either by specifically setting it, in the below we create an array of ints with the size 10:
var arrayOfInts = new int[10];
We can also create an array by declaring it with items, using an object initialiser:
var dateTimes = new DateTime[] {
DateTime.Now,
DateTime.Now.AddDays(1)
};
In the above we create an array with the current date and time and another one with the date tomorrow. If we check the length of the above array it will be 2:
//length is 2
var length = dateTimes.Length;
In order to access the date of tomorrow we can access the array using an index, for the second item that would be 1 as the index starts from 0:
//Getting the second item of the array
var dateTimeOfTomorrow = dateTimes[1];
This also means that it would be natural to iterate through an array using an index:
var dateTimes = new DateTime[] {
DateTime.Now,
DateTime.Now.AddDays(1)
};
for (var i = 0; i < dateTimes.Length; i++)
{
var date = dateTimes[i];
}
There is no way to "add" items to an array. If we use our dateTimes
array with 2 items, then we cannot add a third item. What we can do instead is create a new Array with the size of 3. We can however change what is on a specific position within the array, below we change the date of tomorrow to the day after tomorrow:
var dateTimes = new DateTime[] {
DateTime.Now,
DateTime.Now.AddDays(1)
};
var dayAfterTomorrow = DateTime.Now.AddDays(2);
dateTimes[1] = dayAfterTomorrow;
These were the basics of arrays, we will move on to lists next!
Lists
The biggest difference between lists and arrays is that lists do not have a fixed length. It should be mentioned that internally a List uses an array to store items and this array of course has a fixed length, but using a List you will not have to deal with sizing yourself as the List handles the resizing. A list is often defined with a generic type, for example a list of strings would be instantiated the following way:
var listOfStrings = new List<string>();
You will notice the lack of size of the list and compared to an array we can add items to the list without having to worry about the size of it:
var listOfStrings = new List<string>();
listOfStrings.Add("string1");
listOfStrings.Add("string2");
Just as you can add items to a List you can also remove them and the Count attribute of the list will change depending on this:
var listOfStrings = new List<string>();
listOfStrings.Add("string1");
listOfStrings.Add("string2");
//Will be 2
var numberOfAdded = listOfStrings.Count;
listOfStrings.Remove("string1");
//Will be 1
var numberOfAddedAfterRemoval = listOfStrings.Count;
This is not the "size" of the list, but how many elements are in it. Often you will iterate through a list using foreach:
foreach (var s in listOfStrings)
{
//s will be a new item for each iteration
}
If you want to, you can still access a list using an index:
var listOfStrings = new List<string>();
listOfStrings.Add("string1");
listOfStrings.Add("string2");
var string2 = listOfStrings[1];
The above is an uncommon use case, I included it to show that there is quite some overlap between the functionality of the Array and the List, further down the page we will look into when to what.
Going back and forth
You can easily create an array from a list or a list from an array using the ToList or ToArray methods, both can be seen below:
var dateTimesArray = new DateTime[] { DateTime.Now, DateTime.Now.AddDays(1) };
List<DateTime> dateTimesAsList = dateTimesArray.ToList();
var listOfStrings = new List<string> { "string1", "string2" };
string[] arrayOfStrings = listOfStrings.ToArray();
You might be wondering when to use what and the performance implications of the above. we will look at that next.
When to use what
With the introduction of LINQ and Lambdas in C# a lot of methods are the same when working with arrays and lists. Such as OrderBy, Where or Select to mention a few.
In short if you are working with collections of varying sizes and adding or removing items from the collection you should be using a List. Alternatively if you are working with a collection with a fixed size use an Array. As mentioned here it is very rare you would have to use an Array. It seems that the standard response in the .Net community is to "Use a list unless you have a good reason to use an Array".
The performance difference is often neglible, so in most scenarios it should not be the reason to choose one over the other..
That is all
I hope you found this short post on the differences between array and lists useful. Feel free to leave a comment down below with your thoughts!