Reputation: 315
I create 2 jagged arrays inside my function:
double[][] x = new double[i][];
double[][] y = new double[j][];
I perform some sort of operations on them, and want to return both of them as a result of the function. How can I do that?
Upvotes: 0
Views: 2418
Reputation: 180
Tuples are a completely valid option as well. Some folks don't like them as a matter of opinion, and they're NOT great options for public APIs, but they're useful without adding yet another class to your namespace. You can go overboard with them, and four- and five-tuples exceed even my tolerance for them
double[][] x = new double[i][];
double[][] y = new double[j][];
// Work your magic
return new Tuple<double[][], double[][]>(x, y);
Item1 and Item2 will now be your double[][] arrays.
Upvotes: -1
Reputation: 152521
Well you could return an array of jagged arrays: double[][][]
public double[][][] GetData(int i, int j)
{
double[][] x = new double[i][];
double[][] y = new double[j][];
return new [] {x, y};
}
but it may make more sense to define a class to give the results context. If you return two arrays what do they mean? Are they always in the same order? By just returning an array you leave a lot for the consumer to learn about the meaning of the return type. A class, on the other hand, would provide context:
public TwoArrays GetData(int i, int j)
{
double[][] x = new double[i][];
double[][] y = new double[j][];
return new TwoArrays {X = x, Y = y};
}
public class TwoArrays
{
public double[][] X {get; set;}
public double[][] Y {get; set;}
}
Upvotes: 4