An array of C#, PHP, and HTML programming articles, tutorials, and resources

To calculate the standard deviation of a List of numbers in C#, we can solve it it essentially four steps. Each of them are listed by color below:

  1. Finding the average of a list of number.
  2. Square the value of every number together, and add them together.
  3. Take the result from Step 2 and divide by the number of values in the list.
  4. Take the result from Step 3 and subtract by the square of the average. Because this is the result, we also return the value here.

Below, we have the actual function that performs the operation. The colors coordinate with the steps previously mentioned above.

private double getStandardDeviation(List<double> doubleList)
{
   double average = doubleList.Average();
   double sumOfDerivation = 0;
   foreach (double value in doubleList)
   {
      sumOfDerivation += (value) * (value);
   }
   double sumOfDerivationAverage = sumOfDerivation / doubleList.Count;
   return Math.Sqrt(sumOfDerivationAverage - (average*average));
}

Leave a Reply