2

I was going to use a standard array, I need it to x number of rows and be 2 columns

double[,] members = new double[x, 2];

and loop through all the results I get back and then add them to the array, but as the number of results can change I don't want to pre-define the size of the array...

members[0, 0] = cost;
members[x, 1] = tax;
x++;

I was looking at resizing the array but would it be easier just using an arraylist or list of lists for this?

4 Answers 4

6

I would have a list of some type, where that type has a cost and a tax.

If space is a premium, it could be a struct with two doubles (btw, would decimal be more appropriate? It is almost always is a better choice for money), but if so: make it immutable.

For example:

public struct YourType {
    private readonly decimal cost, tax;
    public decimal Cost { get { return cost; } }
    public decimal Tax { get { return tax; } }
    public YourType(decimal cost, decimal tax) {
        this.cost = cost;
        this.tax = tax;
    }
}

With:

List<YourType> list = new List<YourType>();
Sign up to request clarification or add additional context in comments.

Comments

2

Perhaps the number of items in the second dimension is fixed, or at least, is predictable, so if you're using Microsoft .NET Framework 4.0, you can use a list of tuples:

List<Tuple<double, double>> list = new List<Tuple<int, int>>();

// In your loop...
list.Add(Tuple.Create(cost, tax));

// ... later when retrieving some tuple...
int item0cost = list[0].Item1;
int item0tax = list[0].Item2;

You can always implement some kind of value object class which has Cost and Tax properties, but using tuples you can do it as easy as I shown you in my code sample.

1 Comment

Great, I believe your use case is simple enough for using tuples instead of value objects.
1

You should use a List<List<int>>.

1 Comment

There are known to be exactly 2 columns; going "jagged" is a very expensive way to do this
1

Use a List<T>.

From msdn:

ArrayList has been replaced by List<T> which is faster, and has more features.

T could be a List<double> or a double[2] or a struct or a 2-tuple... but as stated by Marc, using a List<List<double>> is over-doing things if the nested List is destined to always have two items.

3 Comments

There are known to be exactly 2 columns; going "jagged" is a very expensive way to do this
@Marc A 2-tuple is just as expensive as a jagged array. And of course there is the possibility to create a jagged array the other way round, which would be relatively cheap. But still I'd go with a struct like you suggest. Even if it's just to get meaningful property names.
@CodeInChaos I didn't mention a tuple; if you mean "because it is a reference type", then sure, that is true of Tuple<,>, but that isn't the only kind of tuple. Actually, an array is marginally more expensive due to storing the length. Oddly enough, thinking about it, you could just use a double[] and double the length, so item "3" is actually composed of arr[6] and arr[7].

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.