I have a class with a method that generates some results and populates both a List and a double[,]. So when doing the calculations, final results are stored into a double[,] but intermediate results in a List.
Something like this:
public double[,] methodOne(Dictionary<string, double> myInputOne, List<double> myInputTwo)
{
List<double> intermediateResults= new List<double>();
double[,] finalResults= new double[rows, cols];
// Do some calculation using myInputOne and myInputTwo
intermediateResults.Add(something);
// Continue the calculation using also intermediateResults
// store results in double[,] finalResults
return results ;
}
This would work only if you want the method to return the results table. But how can I tell the method to return also the List intermediateResults?
I would like this method to return both the intermediate and final results so that they can be used by other methods down the road. Thank you.
Rather than return a single type or a tuple, return a custom object as it will make the calling code more readable:
public class Results
{
public List<double> IntermediateResults { get; set; }
public double[,] FinalResults { get; set; }
}
Then your method looks something like:
public Results methodOne(Dictionary<string, double> myInputOne, List<double> myInputTwo)
{
List<double> intermediateResults = new List<double>();
double[,] finalResults = new double[rows, cols];
// Do some calculation using myInputOne and myInputTwo
intermediateResults.Add(something);
// Continue the calculation using also intermediateResults
// store results in double[,] finalResults
var results = new Results();
results.FinalResult = finalResult;
results.IntermediateResults = intermediateResults;
return results;
}
You could change the return type to Tuple<T1, T2>
So the method declaration would become
public Tuple<List<double>, double[,]> methodOne(Dictionary<string, double> myInputOne, List<double> myInputTwo)
And in turn, you would return
return new Tuple<List<double>, double[,]>(intermediateResults, finalResults);
However, it depends on how you want to use the returned data. If it's not just a short-term result, you're better off just making a custom object, as Ralph Willgoss suggested.
An option to the other provided answers would be the out -keyword. In your case:
public double[,] methodOne(Dictionary<string, double> myInputOne,
List<double> myInputTwo, out List<double> intermediateResults)
{
intermediateResults= new List<double>();
double[,] finalResults= new double[rows, cols];
// Do some calculation using myInputOne and myInputTwo
intermediateResults.Add(something);
// Continue the calculation using also intermediateResults
// store results in double[,] finalResults
return results ;
}
Usage:
List<double> intermediateResults;
double[,] finalResults = methodOne(someDictionary, someList, out intermediateResults);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.