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?

Community
  • 1
  • 1
william007
  • 17,375
  • 25
  • 118
  • 194

9 Answers9

5
double[] constraintValue = Enumerable.Repeat<double>(-1d,UnUsedServices.Count).ToArray();
D Stanley
  • 149,601
  • 11
  • 178
  • 240
4
for(int i = 0; i < constraintValue.Length; i++)
    constraintValue[i] = -1;
juergen d
  • 201,996
  • 37
  • 293
  • 362
2
 double[] constraintValue = Enumerable.Repeat(-1D, UnUsedServices.Count).ToArray();
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
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
Steve B
  • 36,818
  • 21
  • 101
  • 174
1

you can try with

for(int i =0; i < constraintValue.Length; i ++)
{
constraintValue[i] = -1;
} 
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
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;
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0
for (int i = 0; i < constraintValue.Length; i++)
    constraintValue[i] = -1;
Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
0
    int[] initilize_array = newint[10];

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

    {

    initilize_array[i] = -1;

    }

It's the easiest way

Felipe Ardila
  • 2,995
  • 2
  • 14
  • 12
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);
MrDosu
  • 3,427
  • 15
  • 18
  • 1
    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. – Steve B Oct 12 '12 at 15:14
  • 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. – Servy Oct 12 '12 at 15:16
  • 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. – MrDosu Oct 12 '12 at 15:34