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.
views:
640answers:
6
+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-
2009-07-09 15:04:18
"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
2009-07-09 15:13:22
Sorry... my english is not very well
Jhonny D. Cano -Leftware-
2009-07-09 15:14:58
Bear in mind that this will only work in VS2008/C#3/VB9 and not in the earlier versions (VS2005/C#2/VB8)
CraigTP
2009-07-09 15:19:39
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-
2009-07-09 15:27:18
A:
If you create a new list of int, the starting values of all elements will be zero anyway. No code required.
Christian Hayter
2009-07-09 15:05:43
Wrong; it will default to a zero length with no elements. You're thinking of arrays.
SLaks
2009-07-09 15:09:48
This creates an empty list, not a list of specified size with all entries set to the default value.
Jason
2009-07-09 15:10:30
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
2009-07-09 15:10:54
+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
2009-07-09 15:06:10
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
2009-07-09 15:06:16
this doesn't offer any help with dealing with default values for doubles.
ddc0660
2009-07-09 15:17:07
+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
2009-07-09 15:07:24
+8
A:
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
2009-07-09 15:11:52
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
2009-07-09 16:02:40
@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
2009-07-09 18:52:08