1

This is a C# newbie question.

Consider the following array declaration and initialization:

// This declare the array and initialize it with the default values
double[] MyArray = new double[3];

Somewhere in my code, I initialize the array as follows:

MyArray[0] = 1d;
MyArray[1] = 2d;
MyArray[2] = 3d;

I know that I can assign multiple constants at once to the array at the time of declaration as follows:

double[] MyArray = new double[3] {1d, 2d, 3d};

How to do such an initialization but not at the time of declaration in C#?

In VB.NET, it can be done as follow:

Dim MyArray(3) As Double
...
MyArray = {1#, 2#, 3#}  ' Assign multiple values at once

Update

The reason why I want to separate between declaration and initialization is that the initialization will be inside a loop.

2
  • If you're adding items in a loop, it's very likely that an array is a poor choice for this, and a List<double> will be a better option. Commented Jun 18, 2017 at 0:23
  • @Joel Coehoorn, I'm not adding items in a loop but initializing and updating items in a loop. Commented Jun 18, 2017 at 8:51

4 Answers 4

1

I know that I can assign multiple constants at once to the array at the time of declaration as follows:

double[] MyArray = new double[3] {1d, 2d, 3d};

This code doesn't really assign all the values at once... it actually does the same thing as this:

double[] MyArray = new double[3];
MyArray[0] = 1d;
MyArray[1] = 2d;
MyArray[2] = 3d;

There's no specific syntax to assign multiple values to an existing array.

If you need to do it in a loop, you can do this:

double[] MyArray = new double[3];
for (int i = 0; i < MyArray.Length; i++)
{
    MyArray[i] = i + 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Declaration:

double[] myArray;

Initialization:

myArray = new double[] { 1d, 2d, 3d };

3 Comments

I suspect when such an initialization is done within a loop, the array is allocated in every iteration, which will degrade performance. Doesn't it?
None of your code has a loop anywhere; if you have a different question that has a loop in it, you should ask that.
Sorry if I was not clear enough, I thought the clarification that the initialization might be inside a loop was not needed because when I said "Somewhere in my code ...", it means the initialization can be either inside or outside a loop.
0

Another option--initialize a different source array and then copy that data over your real array each time around the loop.

Comments

0

Probably the simplest way to do it is with a for loop like this

int[] myArray = new int[5];

for (int i = 0; i < myArray.Length; i++)
{
    myArray[i] = i+5;
}

You could also do something like this

int[] myArray = Enumerable.Range(5, 5).ToArray();

But as this article says Enumerable.Range is slower.

EDIT

In response to your update I'm really not sure why you can't do it as part of the initialisation - "constant" suggests that it doesn't change and that you know what it is - if this is the case what's stopping you assigning them when you initialise the array?

Like this

double[] myArray = new double[] { 1d, 2d, 3d };

Or for named constants like this

double[] myArray = new double[] { constA, constB, constC };

If you really want to do it as two parts you could do it like this

double[] myArray;

and

myArray = new double[] { 1d, 2d, 3d };

or

myArray = new double[] { constA, constB, constC };

2 Comments

sorry if was not clear enough. I meant was how to assign multiple "constants", not "values". I will update the question accordingly.
I need to update the values of the array. There are actually twelve elements of the array. Each element represents certain value for particular month. Everytime the array need to be updated, I preinitialize the array with some default values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.