Reputation:
I have to add about 3000 Elements to more than 20 Lists. The code that I am using:
LastChange = new List<string>();
LastChange.AddRange(Enumerable.Repeat("/", 3000));
same for more than 20 Lists...
Is there any "fastest way" to add "/" to the Lists or that is the best solution. Thanks in Advance...
Upvotes: 1
Views: 746
Reputation: 186668
A bit better solution is to allocate memory for 3000
items in the constructor in order to avoid memory reallocation:
LastChange = new List<string>(3000);
LastChange.AddRange(Enumerable.Repeat("/", 3000));
You can go further and get rid of IEnumerator<T>
in Enumerable.Repeat
:
int count = 3000;
LastChange = new List<string>(count);
// compare to 0 is a bit faster then 3000
for (int i = count - 1; i >= 0; --i)
LastChange[i] = "\";
Upvotes: 3