So I have a function
public int Sum(var valueA, var valueB, var valueC) {
var summ = valueA + valueB;
return summ;
}
I want to add to summ valueC if it was given. And let user not specify it if he does not want to. How to do such thing?
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.
In addition to the options that Shane Fulmer provided, you can also use the params keyword to have a function that takes a variable number of parameters:
Of course if you want to limit them to exactly two or three then you probably want to look at optional parameters in C#4.0 or overload the Sum function – by this I mean create two Sum functions, one that takes two parameters and another that takes 3 parameters.