1

What's the best way to build an array?

I'm writing a method that adds a bunch of stuff to an array (of strings) and then returns it. Right now, I've just used a List<string> and then .Add stuff to it, and then return list.ToArray(). I don't know the length of the array beforehand (unless of course I use a 2-pass algorithm, once just to compute the size). Is that a good way to do it, or is there a more efficient way?

PS: I don't want to yield an enumerable.

2 Answers 2

3

If you want to build an array of unknown size, your current approach is perfect. Just add elements to a List<T> and call ToArray() at the end.

The one thing you could, potentially do, however, is "guess" at the final size. If you know you'll be adding a certain range of elements, constructing the list to that appropriate capacity (or slightly larger) may prevent or reduce reallocations during the construction process.

For example, if you suspect that you'll have about 100 elements, you would be better off doing:

var temporaryList = new List<string>(120);

Otherwise, the list will need to resize itself as items are added.

Sign up to request clarification or add additional context in comments.

5 Comments

I have an upper bound, should I use that? It'll rarely reach that upper bound (probably closer to n/2 or n/3) though.
@Mark: It'll be more efficient, typically, to use it than to have reallocations - it'll allocate one handle per element up front, though - so if the upper bound is in the millions, it may not be worth it. (Normally, it starts small, and goes in multiples of 2x each time it reallocates - using an upper bound is usually the best option)
n/2 is a good choice, since it is only one expansion away from the max. Of course, this doesn't replace actual measurement, which would be needed in cases of very large lists.
@codekaizen: True - if the upper bound is small, I'd just use it. If it's large, n/2 (or n/4) is a great option.
@Mark, I think the initial size is better to be the mean/median of normal use cases. Upper bound in most case could be wasting memory.
0

I'd suggest this is an interface problem. Instead of returning an array, return either an IEnumberable<string> or IList<string> (arrays implement IList<T>, so from an implementation detail, an array and List<T> would be indistinguishable).

If you're stuck with an array in the interface and don't know the size ahead of time, your existing solution is the best solution of which I'm aware.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.