Is there a compact manner in which the following can be done?
List<int> a = new List<int>();
for (int i = 0; i < n; ++i)
a.Add(0);
i.e., creating a list of n elements, all of value 0.
Enumberable.Repeat
would be the shortest method I can think of:
var a = Enumerable.Repeat(0, n).ToList();
You can use the Enumerable.Repeat
generator:
var list = new List<int>(Enumerable.Repeat(0, n));
List<int> x = Enumerable.Repeat(value, count).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With