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?
Asked
Active
Viewed 1.3e+01k times
74

Peter Mortensen
- 30,738
- 21
- 105
- 131

JasCav
- 34,458
- 20
- 113
- 170
-
this is doable, but lists in .net don't have a defined size, so its kind of pointletss – DevelopingChris Jul 09 '09 at 15:06
6 Answers
89
In addition to the functional solutions provided (using the static methods on the Enumerable
class), you can pass an array of double
s 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
- 27,796
- 4
- 47
- 63
-
8I suspect it will actually perform slightly worse. The `List
` 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 – SLaks Jul 09 '09 at 16:02`. (8, IIRC) -
@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 Jul 09 '09 at 18:52
-
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. – amalgamate Jul 17 '13 at 21:02
-
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
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. – N0thing Aug 17 '14 at 06:50 -
1Except that it copies the values which are already created in the array. Horrible for performance critical applications. – Christo S. Christov Nov 28 '15 at 00:58
69
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.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Jhonny D. Cano -Leftware-
- 17,663
- 14
- 81
- 103
-
4"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 Jul 09 '09 at 15:13
-
2Bear in mind that this will only work in VS2008/C#3/VB9 and not in the earlier versions (VS2005/C#2/VB8) – CraigTP Jul 09 '09 at 15:19
-
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- Jul 09 '09 at 15:27
46
Use this code:
Enumerable.Repeat(0d, 25).ToList();
new List<double>(new double[25]); //Array elements default to 0

Peter Mortensen
- 30,738
- 21
- 105
- 131

SLaks
- 868,454
- 176
- 1,908
- 1,964
22
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
- 236,483
- 35
- 423
- 525
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.

Daniel
- 321
- 3
- 10
-3
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
- 10,630
- 28
- 36
-
1this doesn't offer any help with dealing with default values for doubles. – ddc0660 Jul 09 '09 at 15:17