In which situations in C# 5.0 would you use one over the other: optional parameters versus method overloads?
-
One quirk is optional parameters must be at the end of the list, so if this order semantically doesn't make sense, or if you want to use a `params`. – lc. Nov 09 '12 at 15:25
-
3The same question was asked here http://stackoverflow.com/questions/3316402/method-overloading-vs-optional-parameter-in-c-sharp-4-0 – Stefan P. Nov 09 '12 at 15:27
-
1possible duplicate of [Should you declare methods using overloads or optional parameters in C# 4.0?](http://stackoverflow.com/questions/251868/should-you-declare-methods-using-overloads-or-optional-parameters-in-c-sharp-4-0) – O. Jones Nov 09 '12 at 15:46
6 Answers
if the internal implemantaion of the function is quite differnet ,then use method overloading.Otherwise, use optional parameters.
Optional parameters methods allow you to write a single testing method instead of writing a testing method for every overloaded method

- 22,907
- 14
- 56
- 77
Well optional parameters should be used when you want to execute a method and not have the caller specify all the parameters.
eg.
private void method (int a, int b = 0){}// if B is optional
Method overloading should be used when you are changing the types of the parameters in a method call.
private void method (double a, int b = 0){}// type of parameter a is different - use overload
You can use both so it is not an either/or. They both have very different purposes.

- 2,524
- 20
- 34
I find that when im adding a switch to an already simple method I would use an optional parameter e.g
public List<Stuff> GetItems (Guid stuffID, bool includeDeleted = false)
{
//implementation
}
The advantage is that the GetItems method could have already been used extensively in other areas of the application, and you dont want to have to go and add in your parameters to each use.
Optional parameters become a problem when you have a method with a large number of parameters, and managing which are optional and which aren't starts to be a chore. In that case you need to start naming the parameters that you are providing. I find that to a bit of a mess
Anything more complicated than the above example should really use overloads. I cant think of a single example where I would use more than one optional parameter either.

- 323
- 2
- 13
As pointed out, optional parameters must be at the end of the list. But for the most part, it really doesn't matter.
There are indications that the only reason so many of the .NET classes use overloads is because optional parameters were not available when those classes were written. So I generally find optional parameters a little simpler to write than multiple versions of my methods.
But, again, I really don't see any right or wrong way here.

- 65,341
- 71
- 269
- 466
Method overloads can be replaced by optional parameters in a few circumstances, but they are not totally the same. Sometimes we still need to use method overloads, for example:
void Foo()
{
Foo(DateTime.Now);
}
void Foo(DateTime dt)
{
//do something
}
//you can't use optional parameters here because DateTime.Now is not a constant
void Foo(DateTime dt = DateTime.Now) //compile error
{
//do something
}
For some scenarios both are ok, I'd prefer optional parameters, because it makes the code shorter and clearer.

- 42,509
- 16
- 113
- 174
i think it doesn't matter using optional parameters instead of overloading but i have to say i faced a problem using optional parameters due to their "constant constraint".
I had a parameter of type DateTime
.
I wanted to use a function with the following signature:
void AddItem(string itemCode, Datetime defaultUpdateTime = DateTime.Now
but i couldn't do it becouse at compile time i got `
“optional parameters must be a compile-time constant
So you must consider that with overloading you can work around problems like this one using two different signature and opportunaly initializing the parameter.

- 1,490
- 2
- 12
- 22