Auto-initializing C# lists

107,631

Solution 1

In addition to the functional solutions provided (using the static methods on the Enumerable class), you can pass an array of doubles in the constructor.

var tenDoubles = new List<double>(new double[10]);

This works because the default value of an double is already 0, and probably performs slightly better.

Solution 2

You can use the initializer:

var listInt = new List<int> {4, 5, 6, 7};
var listString = new List<string> {"string1", "hello", "world"};
var listCustomObjects = new List<Animal> {new Cat(), new Dog(), new Horse()};

So you could be using this:

var listInt = new List<double> {0.0, 0.0, 0.0, 0.0};

Otherwise, using the default constructor, the List will be empty.

Solution 3

Use this code:

Enumerable.Repeat(0d, 25).ToList();
new List<double>(new double[25]);     //Array elements default to 0

Solution 4

One possibility is to use Enumerable.Range:

int capacity;
var list = Enumerable.Range(0, capacity).Select(i => 0d).ToList();

Another is:

int capacity;
var list = new List<double>(new double[capacity]);

Solution 5

A bit late, but maybe still of interest: Using LINQ, try

var initializedList = new double[10].ToList()

...hopefully avoiding copying the list (that's up to LINQ now).

This should be a comment to Michael Meadows' answer, but I'm lacking reputation.

Share:
107,631
JasCav
Author by

JasCav

Updated on July 05, 2022

Comments

  • JasCav
    JasCav almost 2 years

    I am creating a new C# List (List<double>). Is there a way, other than to do a loop over the list, to initialize all the starting values to 0?

  • Andrew Hare
    Andrew Hare almost 15 years
    I think this is more what the OP is looking for.
  • ddc0660
    ddc0660 almost 15 years
    this doesn't offer any help with dealing with default values for doubles.
  • DMpal  Jain
    DMpal Jain almost 15 years
    Bear in mind that this will only work in VS2008/C#3/VB9 and not in the earlier versions (VS2005/C#2/VB8)
  • Jhonny D. Cano -Leftware-
    Jhonny D. Cano -Leftware- almost 15 years
    Well, that's right... I just assumed it because he mentioned a C# List and not an ArrayList or something like that; but i'ts worth to mention it
  • jason
    jason almost 15 years
    The first should be var list = Enumerable.Repeat(0d, 25).ToList();
  • SLaks
    SLaks almost 15 years
    I suspect it will actually perform slightly worse. The List<T> constructor copies its array, so this method allocates two arrays. The LINQ method uses an iterator without allocating much memory. On the other hand, giving it an array will allow it to use the correct size, which will save an array resize if there are more elements than the default capacity for List<T>. (8, IIRC)
  • Michael Meadows
    Michael Meadows almost 15 years
    @SLaks, interesting point. I suppose you're correct. It probably depends on the initial size. For smaller initial sizes, the functional approach is probably slightly more efficient, where larger initial sizes would definitely perform better with the array initializer. In the end, though, I suppose the performance assertion is probably academic at best, since it will likely never make a difference in a real production app.
  • amalgamate
    amalgamate almost 11 years
    This is slightly off from the question but I think that it may be useful for some of those who initially come to this question for help. You can change the capacity of the list at any time in order to maximize efficiency. NameOfList.Capacity = 5000; // sets the size of the list to 5000 without actually adding to or removing from the list.
  • N0thing
    N0thing over 9 years
    Keep in mind that for large lists, that is not recommended since you will initialize two arrays of 10 (the new double[10] will generate 10 doubles, the List<double> will the copy it internally). For 10 it is fine, but let's say you had to initialize an array of 1e6 elements, than would would end-up using 2x the time / memory to do so.
  • Christo S. Christov
    Christo S. Christov over 8 years
    Except that it copies the values which are already created in the array. Horrible for performance critical applications.