Fill List with default values? [duplicate]

Well, you can ask LINQ to do the looping for you:

List<int> x = Enumerable.Repeat(value, count).ToList();

It’s unclear whether by “default value” you mean 0 or a custom default value.

You can make this slightly more efficient (in execution time; it’s worse in memory) by creating an array:

List<int> x = new List<int>(new int[count]);

That will do a block copy from the array into the list, which will probably be more efficient than the looping required by ToList.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)