Instead of returning an
int
, return a
string
and add the string parameter at the start:
public string sum(string name, params int[] marks)
{
int sum = 0;
foreach (int element in marks)
{
sum += element;
}
return String.Concat(name, " ", sum);
}
Also, if you use .NET 3.5 or later, it is not necessary to take the sum using a foreach loop, then you can just use the
Sum[
^] method:
public string sum(string name, params int[] marks)
{
int sum = marks.Sum();
return String.Concat(name, " ", sum);
}