3

I got an int-array with multiple values. I have to add an element to that array. I would solve it that way:

int[] myIntArray=SomeMagicThere();

List<int> intList= myIntArray.ToList();
intList.Add(88);
myIntArray= intList.ToArray();

(This is quite simplified, there are good reasons why I cannot change the type of myIntArray)

Would you solve it in another way? It feels a bit "dirty" to do two casts

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • 1
    What's the reason for needing it in an array in the first place? – Arran Jun 07 '13 at 09:02
  • 1
    Why not use a List in the first place? Does it really have to be an array? – Mithrandir Jun 07 '13 at 09:05
  • As I wrote: This is a simplyfied example. I am accessing an existing object-structure created from an entity framework. No good idea to change it just for that purpose :) – Ole Albers Jun 07 '13 at 09:39

4 Answers4

9

The canonical way would be

Array.Resize(ref myIntArray, myIntArray.Length + 1);
myIntArray[myIntArray.Length - 1] = 88;

A LINQ version that doesn't perform quite so many array copies as yours would be

myIntArray = myIntArray.Concat(new[] { 88 }).ToArray();

EDIT: This LINQ version is actually slower than your original! LINQ array-to-list and list-to-array is optimized to a direct array copy, whereas the Concat means LINQ no longer knows how long the sequence is and thus results in multiple array resizes and copies as the internal buffer's capacity is reached.

(I'm assuming you have to use an array, as you seem familiar with lists already.)

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • 1
    I really like the LINQ-approach, even if it just looks better :). Thanks – Ole Albers Jun 07 '13 at 09:42
  • 1
    @Ole Just so you know, the LINQ version is about ten times slower than the `Array` version. In fact, it's slower than your original, and now I'm going to have to find out why. Probably LINQ list-to-array and array-to-list has some nice optimization, whereas `Concat` results in a genuine, slow iterator loop being set up. – Rawling Jun 07 '13 at 09:52
2

Would you solve it in another way?

I'd change SomeMagicThere (of course, if it is possible):

public List<int> SomeMagicThere()
{
   // ...
}

There should not be any performance lag, because internally List<T> uses array to store its items. In fact, it does array resizing for you, and I can't see any reason to write the same code, that List<T> already has.

Dennis
  • 37,026
  • 10
  • 82
  • 150
0

You basically have two options:

  1. If you change the size frequently - consider using List< int> instead of Array.
  2. Use Array.Resize static method.

Arrays are fast for accessing elements by index, while List<> is fast for adding/removing elements.

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
0

The offending line is the first:

int[] myIntArray=SomeMagicThere();

Change to:

List<int> magicInts = new List<int>(SomeMagicThere());

Then you can easily call:

magicInts.Add(88);

The List<T>(IEnumerable<T>) constructor will copy the input into itself, allowing you to easily add and remove items without ever having to modify the array yourself.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272