I would like to know an alternative way to write this code without using a Linq Method Average().
double[] array1 = { 1, 2, 3, 5, 0 };
double average1 = array1.Average();
Console.WriteLine(average1);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No idea why you wouldn’t want to use the LINQ Average function but you could loop through the elements of the array in order to calculate the sum and finally divide by the number of elements:
Now that you look at this code it is clear why the following is much better: