user147504
user147504

Reputation: 175

Create a C# list of size n and initialize it with 0's

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

Answers (1)

Bruno Belmondo
Bruno Belmondo

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

Related Questions