Not really an answer, more a research comment.
I ran into the same problem and did a quick test.
I tried with the code below and could not get this code to throw the ArgumentException: Destination array was not long enough
. But when I remove the .ToList()
from the line
return allLines.ToList().ToArray();
it immediately crashes.
This is the demo code and even the IDE tells me, I should remove the ToList()
call as it seems redundant.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main() {
List<string> thelist = new List<string>();
Thread producer = new Thread(() => {
while (true) {
thelist.Add("a" + DateTime.Now);
}
});
Thread transformer = new Thread(() => {
while (true) {
string[] thearray = thelist.ToList().ToArray();
Console.WriteLine(thearray.Length);
}
});
producer.Start();
transformer.Start();
Console.ReadKey(true);
}
}
}
I really wonder, why it would not crash, as the List is also backed by an array.