One reason is you have class acting as cache for e.g. One class has field listOfAllClients and you want to return subset as below
List<Clients> _listOfClientCache ;
private client[] GetSClientsByCity(string city)
{
_listOfClient.Where(x => x.Client.City = city).ToArray();
}
One reason I see is memory foot print, If caller is not going to add or remove elements then it saves memory.
List is implemented using arrays, but when need to add element more than size of underline array then it just doubles the size of underlying array and copy elements. So there is good chance that underlying array size more than actual elements within the List.
Converting to Array will free up memory allocated for the unused elements.
Below is the test
[TestMethod]
public void ListAndArrayTest()
{
int[] array = GetArray();
Debug.WriteLine("IsFixedSize {0}, Elements {1}", array.IsFixedSize, array.Length);
// This will print IsFixedSize True, Elements 35
}
private int[] GetArray()
{
List<int> localList = new List<int>();
for (int i = 0; i < 35; i++)
{
localList.Add(i);
}
Debug.WriteLine("Capacity {0}, Element Count {1}", localList.Capacity, localList.Count);
// This will print Capacity **64**, Element Count **35**
return localList.ToArray();
}
Output : Capacity 64, Element Count 35
IsFixedSize True, Elements 35
From the output it is clear that caller has pure array, not any reference of original list which was using array under the hood with capacity of 64 element as after 32 elements when it required more elements it doubled size of array to 64 and copied the elements to it.
Once it is converted back to array caller has array of 35 element.