-2

I am developing a Calculations library in c# using Math.Net library. I have several methods in the library that would be receiving IEnumerable double list of values. I need to calculate the annual and monthly values and return those to the caller method. Could somebody tell me if this is the right way of doing it. I chose to use dictionary object

public Dictionary<string, double> ArithmeticMean(IEnumerable<double> ReturnsList)
{
    Dictionary<string, double> arithmethicMean = new Dictionary<string, double>();
    var returnList = ReturnsList.Mean();
    arithmethicMean.Add("Monthly", returnList);
    arithmethicMean.Add("Annual", returnList * Math.Pow(12, 0.5));

    return arithmethicMean;
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Tom
  • 8,175
  • 41
  • 136
  • 267
  • 3
    Aside from the very counter-intuitive variable names, does this not work? Does it give erroneous results? What problem are you trying to solve? – Mark Benningfield Apr 06 '19 at 14:24

1 Answers1

3

Your approach is relaying on magic strings. It is better to use a separate type for the result like:

public class ArithmeticMeanResult
{
    public double Monthly;
    public double Annual;
}

and then return it:

return new ArithmeticMeanResult()
{
    Monthly = returnList,
    Annual = returnList * Math.Pow(12, 0.5)
};

If you are using C# 7 then you can Tuples:

public (double Monthly, double Annual) ArithmeticMean(IEnumerable<double> ReturnsList)
{
    ...
    return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
}
AlbertK
  • 11,841
  • 5
  • 40
  • 36