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.
-
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.
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.Jhonny D. Cano -Leftware- : Sorry... my english is not very wellCraigTP : 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 -
If you create a new list of int, the starting values of all elements will be zero anyway. No code required.
SLaks : Wrong; it will default to a zero length with no elements. You're thinking of arrays.Jason : This creates an empty list, not a list of specified size with all entries set to the default value.Steve Guidi : 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.Christian Hayter : Oops, my mistake. I vote for Mike's answer then.From Christian Hayter -
Enumerable.Repeat(0d, 25).ToList(); new List<double>(new double[25]); //Array elements default to 0EDIT: Changed
inttodoubleto match the question2nd EDIT: Fixed first call
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();SLaks : Fixed; thanks.From SLaks -
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
ddc0660 : this doesn't offer any help with dealing with default values for doubles.From Colin -
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]);Stan R. : this is more like it.Nathen Silver : This is good because the 2nd example can be used in C# 2.0.From Jason -
In addition to the functional solutions provided (using the static methods on the
Enumerableclass), you can pass an array ofdoubles in the constructor.var tenDoubles = new List<double>(new double[10]);This works because the default value of an
doubleis already 0, and probably performs slightly better.SLaks : I 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 `. (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.Steve Guidi : A very clever and efficient solution.From Michael Meadows
0 comments:
Post a Comment