I need to define two methods for returning the sum and average of an int array. The method defining is as follow:-
public int Sum(params int[] customerssalary)
{
// I tried the following but it fails return customerssalary.sum();
}
Another question is, how can I return the average of these int values?
This is the way you should be doing it, and I say this because you are clearly new to C# and should probably try to understand how some basic stuff works!
with this
Sumfunction, you can use this to calculate the average too…the reason for using a
decimaltype in the second function is because the division can easily return a non-integer resultOthers have provided a Linq alternative which is what I would use myself anyway, but with Linq there is no point in having your own functions anyway. I have made the assumption that you have been asked to implement such functions as a task to demonstrate your understanding of C#, but I could be wrong.