21

let's say I have a simple List<bool>. I want to initialize it and add e.g 100 elements to it. To do so, I can do:

var myList = new List<bool>();

for (int i = 0; i < 100; i++)
{
    myList.Add(false);
}

but it's not the most elegant approach. Is there any built-in method to simplify it ? I don't want any loops, just for curiosity

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Tony
  • 12,405
  • 36
  • 126
  • 226
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 13 '14 at 08:26
  • @TimSchmelter I think that's a *little* harsh as that question is asking about initializing multiple different integer values between value A and B. This is just a simple initialize a `List` with false/default values. – CodingIntrigue Mar 13 '14 at 08:34
  • I'm getting mentally stuck on what is fastest way to do this, despite knowing its premature optimization and not the actual question. – Jodrell Mar 13 '14 at 12:23

5 Answers5

47

Using Enumerable.Repeat

var myList = Enumerable.Repeat(false, 100).ToList();

which

Generates a sequence that contains one repeated value.

dee-see
  • 23,668
  • 5
  • 58
  • 91
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
17

false is easy, since it's the default value of boolean:

new List<bool>(new bool[100]);
Luaan
  • 62,244
  • 7
  • 97
  • 116
13

You could use LINQ and more specifically the Select and ToList extension methods:

var myList = Enumerable.Range(1, 100).Select(x => false).ToList();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
8

List<T> has no specific method to do this. The loop is your best option.

However, you can make it more efficient at runtime, by initializing the list with an initial capacity:

var myList = new List<bool>(100);

When using this constructor, the list will internally allocate an array of 100 elements. If you use the default constructor, it will start of with first 0 and then 4 elements. After 4 items have been added, the list will allocate an array of 8 elements and copy the 4 that were already added over. Then it will grow to 16, 32, 64 and finally 128. All these allocations and copy operations can be avoided by using the constructor with the initial capacity.

Alternatively, if you need to do this in different places in your program, you could make an extension method:

public static void Initialize<T>(this List<T> list, T value, int count)
{
    if (list == null)
    {
        throw new ArgumentNullException("list");
    }

    if (list.Count != 0)
    {
        throw new InvalidOperationException("list already initialized");
    }

    if (list.Capacity < count)
    { 
        list.Capacity = count;
    }

    for (int i = 0, i < count, i++)
    {
        list.Add(value);
    }
}

You would use it like this:

var myList = new List<bool>();
myList.Initialize(false, 100);

The other option that you have is to use an array.

var myList = new bool[100];

The interesting thing about this specific example is that you do not have to initialize the array. Since false is the default value for bool, all elements in the array will automatically have the value false. If your list does not need to resize dynamically, this is certainly an option to consider.

Kris Vandermotten
  • 10,111
  • 38
  • 49
4

You can use List.AddRange:

List<bool> list = new List<bool>();
list.AddRange(Enumerable.Repeat(default(bool), 100));
w.b
  • 11,026
  • 5
  • 30
  • 49