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?
Automatic variables are variables defined inside a function or block of code without the static keyword. These variables have undefined values if you don't explicitly initialize them. If you don't initialize an automatic variable, you must make sure you assign to it before using the value.
Automatic initialization in JavaJava does not initialize non-array local variables (also referred to as automatic variables) . The Java compiler generates error messages when it detects attempts to use uninitialized local variables. The Initializer program shows how automatic initialization works.
Example# In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; automatic variables (including register variables) have indeterminate1 (i.e., garbage) initial values.
You can initialize any auto variable except function parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With