views:

41

answers:

3

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?

+4  A: 

You can do this in C# 4.0 with optional arguments. If you're using a version prior to C# 4.0, you could create an overloaded function that calls your function with a default value.

Shane Fulmer
+3  A: 

If you are using .NET 3.5 or earlier you can use Nullable Types.

public int Sum(int valueA, int valueB, int? valueC)
{
    int sum = valueA + valueB;
    if (valueC.HasValue)
    {
        sum += valueC.Value;
    }

    return sum;
}

The calls would be:

int answer1 = Sum(1, 2, 3); // = 6

int answer2 = Sum(1, 2, null); // = 3

Of course the classic way to do this is to use method overloads:

public int Sum(int valueA, int valueB)
{
    int sum = valueA + valueB;
    return sum;
}

public int Sum(int valueA, int valueB, int valueC)
{
    int sum = valueA + valueB + valueC;
    return sum;
}

int answer1 = Sum(1, 2);
int answer2 = Sum(1, 2, 3);

If you want to be able to use string as well as int either move to .NET4 or create another pair of overloaded methods:

public int Sum(string valueA, string valueB)
{
    // Convert the strings to int and call the previous code
    // You will need to cope with the case when the strings don't represent numbers
}

public int Sum(string valueA, string valueB, string valueC)
{
    ...
}

If you want to cope with mixed string and int then you'll need even more overloads - which is probably overkill.

ChrisF
and how to make it work with strings?
Blender
@Ole - `string` instead of `int` or as well as `int`?
ChrisF
as well as int.
Blender
And if I use Nullable Types can I not say (null) at the end all the time (just skeep it like with overloads) ?
Blender
@Ole If a method parameter is a nullable type, you will need to either send in the type or null. You can't just ignore it, because the method wouldn't match the signature of the method you created and this will get errors that there isn't a method in the class that matches the call.
Waleed Al-Balooshi
@Ole - no you can't skip the arguments pre .NET 4
ChrisF
+1  A: 

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:

public int Sum(params int[] values)
{
    int sum = 0;
    for(int i = 0; i < values.Length; i++){
        sum+=values[i];
    }
    return sum;
}

int answer2Parameters = Sum(1, 5);
int answer3Parameters = Sum(1, 2, 3);
int answer4Parameters = Sum(1, 3, 5, 6);

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.

public int Sum(int valueA, int valueB) { 
  int summ = valueA + valueB; 
  return summ; 
} 

public int Sum(int valueA, int valueB, int valueC) { 
  int summ = valueA + valueB + valueC; 
  return summ; 
}
Waleed Al-Balooshi
And how to use ints [] and strings[] at the same time? and how to specify the names of values in array?
Blender
@Ole there are a number of ways to do this, but based on your requirements, the best design might be different. Are you expecting that you will be sending either int or string "numbers" OR do you want this method to both sum ints and concatenate strings? If the second option is what you are looking for, then it makes more sense to have two methods one called Sum for ints and the other called Concatenate for strings. If on the other hand you are looking for Summing int or string numbers then you can either make the array of object or overload the method.
Waleed Al-Balooshi
@Ole also what do you mean by specify the names of the values in array? The arguments to the method are added to the array parameter called values. values[0] has the first argument values[1] has second argument, and so on. You can manually separate them out of the array in your method code if you want.
Waleed Al-Balooshi