-2

What is the best way to check null or empty for IEnumerable<double> in C#?

What I have tried so far is

return returnList != null && returnList.Any();

but I get a message that this expression will always return true.

Full method:

public double ArithmeticMean(IEnumerable<double> ReturnsList, bool IsMonthly)
{
    var returnList = ReturnsList.Mean();

    if (IsMonthly)
    {
        return returnList;
    }
    else
    {
        return returnList * Math.Pow(12, 0.5);
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Tom
  • 8,175
  • 41
  • 136
  • 267

1 Answers1

0

The Math.NET documentation says of the Mean() function:

https://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Statistics.htm#Mean

Evaluates the sample mean, an estimate of the population mean. Returns NaN if data is empty or if any entry is NaN.

Neither the mean or Double.NaN are null, so returnList is never null, making the check for this redundant as proposed by ReSharper.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • The returnrnlist is an array of double values passed to the method. There could be a possibility that the caller of the method might not pass the returnlist in that case you would be doing mean of nothing – Tom Apr 06 '19 at 14:03