views:

640

answers:

6

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

+10  A: 

well, 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()};

Edited to fit the question.

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.

Jhonny D. Cano -Leftware-
"Otherwise, using the default constructor, the List will be initialized with 0 elements." To be clear, the list will be empty, not that it will have entries all set to the default value.
Jason
Sorry... my english is not very well
Jhonny D. Cano -Leftware-
Bear in mind that this will only work in VS2008/C#3/VB9 and not in the earlier versions (VS2005/C#2/VB8)
CraigTP
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
Jhonny D. Cano -Leftware-
A: 

If you create a new list of int, the starting values of all elements will be zero anyway. No code required.

Christian Hayter
Wrong; it will default to a zero length with no elements. You're thinking of arrays.
SLaks
This creates an empty list, not a list of specified size with all entries set to the default value.
Jason
The list will not have any elements to begin with, so you would need to call Add (or an equivalent method) to insert the zeros. On the other hand, your idea will work with an array.
Steve Guidi
Oops, my mistake. I vote for Mike's answer then.
Christian Hayter
+6  A: 
Enumerable.Repeat(0d, 25).ToList();
new List<double>(new double[25]);     //Array elements default to 0

EDIT: Changed int to double to match the question

2nd EDIT: Fixed first call

SLaks
I think this is more what the OP is looking for.
Andrew Hare
The first should be var list = Enumerable.Repeat(0d, 25).ToList();
Jason
Fixed; thanks.
SLaks
A: 

For more complex types:

List<Customer> listOfCustomers =
        new List<Customer> {
            { Id = 1, Name="Dave", City="Sarasota" },
            { Id = 2, Name="John", City="Tampa" },
            { Id = 3, Name="Abe", City="Miami" }
        };

from here: David Hayden's Blog

Colin
this doesn't offer any help with dealing with default values for doubles.
ddc0660
+2  A: 

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]);
Jason
this is more like it.
Stan R.
This is good because the 2nd example can be used in C# 2.0.
Nathen Silver
+8  A: 

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.

Michael Meadows
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)
SLaks
@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.
Michael Meadows
A very clever and efficient solution.
Steve Guidi