This post will discuss how to create a list of a given size in C#, initialized with some value.

We can generate a sequence of a repeated value with LINQ’s Enumerable.Repeat() method. It takes the value to be repeated and the number of times the value has to be repeated in the resulting sequence. The following code example shows an invocation for this method to create a list using the ToList() method:

Download  Run Code

 
The following example demonstrates the usage of the Enumerable.Repeat() method for generating a list of a repeated value with the List constructor.

Download  Run Code

That’s all about creating a list of a given size in C#, initialized with some value.