tags:

views:

5406

answers:

5

What is the slickest way to initialize an array of dynamic size in C# that you know of?

This is the best I could come up with

private bool[] GetPageNumbersToLink(IPagedResult result)
{
   if (result.TotalPages <= 9)
      return new bool[result.TotalPages + 1].Select(b => true).ToArray();

   ...

Thanks!

Rob

A: 

Untested, but could you just do this?

return result.Select(p => true).ToArray();

Skipping the "new bool[]" part?

Matt Hamilton
only if IPagedResult : IEnumerable<T>
Mark Cidade
+3  A: 

I would actually suggest this:

return Enumerable.Range(0, count).Select(x => true).ToArray();

This way you only allocate one array. This is essentially a more concise way to express:

var array = new bool[count];

for(var i = 0; i < count; i++) {
   array[i] = true;
}

return array;
Nick Aceves
+3  A: 

EDIT: as a commenter pointed out, my original implementation didn't work. This version works but is rather un-slick being based around a for loop.

If you're willing to create an extension method, you could try this

public static T[] SetAllValues<T>(this T[] array, T value) where T : struct
{
    for (int i = 0; i < array.Length; i++)
        array[i] = value;

    return array;
}

and then invoke it like this

bool[] tenTrueBoolsInAnArray = new bool[10].SetAllValues(true);

As an alternative, if you're happy with having a class hanging around, you could try something like this

public static class ArrayOf<T>
{
    public static T[] Create(int size, T initialValue)
    {
        T[] array = (T[])Array.CreateInstance(typeof(T), size);
        for (int i = 0; i < array.Length; i++)
            array[i] = initialValue;
        return array;
    }
}

which you can invoke like

bool[] tenTrueBoolsInAnArray = ArrayOf<bool>.Create(10, true);

Not sure which I prefer, although I do lurv extension methods lots and lots in general.

DotNetGuy
extension methods, FTW!
Mark Cidade
I don't believe your SetAllValues will work: In your lambda expression, x is not passed by reference, so assigning a value to it doesn't change the value stored in the array.
Samuel Jack
Yeah, you're absolutely right. I mentioned I hadn't actually compiled it which would have shown up that rather elementary error. I replaced the ForEach<T> with a simple loop and that works fine, but it's not slick as the questioner demanded.
DotNetGuy
Other than that, your extension method has a signature that implies that it will return a new array, but it modifies the original array and returns that instead. Bad form.
Robert Jeppesen
+11  A: 
Enumerable.Repeat(true, result.TotalPages + 1).ToArray()
Mark Cidade
Now that's just slick!
Rob
@Mark - I think Nigel's performance note warrants a mention - http://stackoverflow.com/questions/136836/c-array-initialization-with-non-default-value/1051227#1051227
CrimsonX
+3  A: 

If by 'slickest' you mean fastest, I'm afraid that Enumerable.Repeat may be 20x slower than a for loop. See http://dotnetperls.com/initialize-array:

Initialize with for loop:             85 ms  [much faster]
Initialize with Enumerable.Repeat:  1645 ms

So use Dotnetguy's SetAllValues() method.

Nigel Touch
+1 for the performance note.
AMissico