Reputation: 175
How can i create a list in C# of variable size and initialize all the elements with 0's?
One way I know is using Enumerable in this way
IEnumerable<int> list1 = Enumerable.Range(1, n).Select(x => x * 0);
Not sure about the time complexity of using this way. Just looking for a better way if any.
Upvotes: 2
Views: 8009
Reputation: 2367
Enumerable.Repeat(0, n);
will be the memory efficient way but as int has 0 as a default value you can also just do
var array = new int[n];
Upvotes: 8