Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class ParamParmeters
{

    public int sum(string name, params int[] marks)
    {
        int sum = 0;
        foreach (int element in marks)
        {

            sum += element;

        }
        return sum;


    }
}
class Program
{
    static void Main(string[] args)
    {


        ParamParmeters pp = new ParamParmeters();

        int result = pp.sum("ABC", 10, 20, 30);


        Console.WriteLine(result);



    }
}


I just get the output has 60 . I need output has "ABC 60"
Posted
Comments
Prasad Khandekar 21-May-14 11:03am    
In that case change the implementation of sum function as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class ParamParmeters
{

public string sum(string name, params int[] marks)
{
int sum = 0;
foreach (int element in marks)
{
sum += element;
}
return (name + sum);
}
}

class Program
{
static void Main(string[] args)
{
ParamParmeters pp = new ParamParmeters();
string result = pp.sum("ABC", 10, 20, 30);
Console.WriteLine(result);
}
}
Regards,
ShaHam11 21-May-14 11:16am    
Thank you i just learnt I can return multiple values with return keyword

You can't return a string from an int method!

Try this:
C#
public string sum(string name, params int[] marks)
{
    int sum = 0;
    foreach (int element in marks)
    {
        sum += element;
    }
    return string.Format("{0} {1}", name, sum);
}

And then:
C#
string result = pp.sum("ABC", 10, 20, 30);
 
Share this answer
 
Comments
Prasad Khandekar 21-May-14 11:04am    
5+
ShaHam11 21-May-14 11:08am    
Thank you OriginalGriff.. I just have quick question i tried using return sum.ToString(); it didnt work it gave me only the sum value not"ABC" but when i used string.format as per your reply i was able to get output as i expected ..why is it unable to get the same result with .tostring();
OriginalGriff 21-May-14 12:06pm    
Because sum.ToString() only converts the integer value "sum" to a string - it doesn't include the "name" variable, any more than your car will automatically go to the garage to get fuel when it "feels a bit low" - you have to notice and drive the car there, and if you want to return the "name" from your method then you have to specifically use it in the string generation process.
Instead of returning an int, return a string and add the string parameter at the start:
C#
public string sum(string name, params int[] marks) // change "public int" into "public string"
{
    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:
C#
public string sum(string name, params int[] marks)
{
    int sum = marks.Sum();
    return String.Concat(name, " ", sum);
}
 
Share this answer
 
v3
Comments
ShaHam11 21-May-14 11:21am    
Thank your programFOx ..it works marks.sum() thank you for sharing it
Thomas Daniels 21-May-14 11:29am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900