I have a comma delimited string called ctext
which I want to split and put into a List<string>
.
Would using LINQ,
List<string> f = ctext.Split(',').ToList();
be slower than not using LINQ?
List<string> f;
f.AddRange(ctext.Split(','));
It seems that LINQ would actually copy something somewhere at some point which would make it slower, whereas AddRange()
would just check the size of the list once, expand it, and dump it in.
Or is there an even faster way? (Like using a for
loop, but I doubt it.)
Fortunately, we can easily look at what ToList
does now that it's open source. (Follow the link for the latest source...)
I haven't seen IListProvider<T>
before, but I doubt that an array implements it, which means we've basically got new List<TSource>(source)
. Looking at the List<T>
source shows that both the constructor and AddRange
basically end up using CopyTo
.
In other words, other than a few levels of indirection, I'd expect them both to do the same thing.
See more on this question at Stackoverflow