0

Possible Duplicate:
How to populate/instantiate a C# array with a single value?

Given double array

   double[] constraintValue = new double[UnUsedServices.Count];

I want to initialize all entry with -1, is there a simple syntax to do it?

2

9 Answers 9

5
double[] constraintValue = Enumerable.Repeat<double>(-1d,UnUsedServices.Count).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

4
for(int i = 0; i < constraintValue.Length; i++)
    constraintValue[i] = -1;

3 Comments

Count is for collections. Replace by Length for arrays
I used the length value from the question
Hmm, this seems to be the intuitive one...i just expect c# might have some syntatic sugar, but it doesn't seems to be.
2
 double[] constraintValue = Enumerable.Repeat(-1D, UnUsedServices.Count).ToArray();

Comments

2

The exact answer to your question is :

for(var i =0; i < constraintValue.Length; i ++)
{
    constraintValue[i] = -1;
} 

However, if you want to have "unset" values, why don't you use Nullable<double> ?

double?[] constraintValue = new double?[UnUsedServices.Count];

constraintValue[10] = 42D;
constraintValue[20] = 0D;

var x1 = contraintValue[10].HasValue; // true
var x1val = contraintValue[10].Value; // 42D

var x2 = contraintValue[20].HasValue; // true
var x2val = contraintValue[20].Value; // 0D

var x3 = contraintValue[10].HasValue; // false
//var x3val = contraintValue[10].Value; // exception

1 Comment

Thanks, learnt new thing here.
1

you can try with

for(int i =0; i < constraintValue.Length; i ++)
{
constraintValue[i] = -1;
} 

Comments

0

Don't know if you would call this "easy":

double[] constraintValue = new double[]{ -1, -1, 
                                         ... (UnUsedServices.Count times)};

And of course, the standard way to do this is to loop over each array item to initialize it:

for(int i = 0; i < constraintValue.Length; i++)
{
    constraintValue[i] = -1;
}

Comments

0
for (int i = 0; i < constraintValue.Length; i++)
    constraintValue[i] = -1;

Comments

0
    int[] initilize_array = newint[10];

    for (int i = 0; i <initilize_array.Length; i++)

    {

    initilize_array[i] = -1;

    }

It's the easiest way

Comments

0

If you need performance faster then Enumerable.Repeat do it in parallel using the TPL:

double[] constraintValue = new double[UnUsedServices.Count];
Parallel.For(0, UnUsedServices.Count, index => constraintValue[index] = -1);

3 Comments

No. Not here. Benchmark it, you will see a large performance degradation. Why? because the parallelized job is so small and cheap. If you add parrallelism, your application will have to workf with threads, context switch, locks, etc. This structural cost is quite larger than the benefits.
And to add to steve's comments, you're also destroying memory localization. Accessing sequential memory locations is much quicker than "random" access to memory, which would be the result of parallelizing it.
Yeah, ignore this. I underestimated how much this scenario is optimized. I benched it against Enumerable.Repeat. Enumerable.Repeat is horribly slow, but the simple for loop in the accepted answer is much faster then both solutions until about 10million entries in the array at which point the Parallel is equally fast as the for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.