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?
- this is doable, but lists in .net don't have a defined size, so its kind of pointletssDevelopingChris– DevelopingChris2009-07-09 15:06:44 +00:00Commented Jul 9, 2009 at 15:06
Add a comment |
6 Answers
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.
5 Comments
SLaks
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
@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
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
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
Except that it copies the values which are already created in the array. Horrible for performance critical applications.
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.
3 Comments
jason
"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.
CraigTP
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-
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
Use this code:
Enumerable.Repeat(0d, 25).ToList(); new List<double>(new double[25]); //Array elements default to 0 2 Comments
Andrew Hare
I think this is more what the OP is looking for.
jason
The first should be var list = Enumerable.Repeat(0d, 25).ToList();
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]); Comments
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
1 Comment
ddc0660
this doesn't offer any help with dealing with default values for doubles.