Advanced C # 5.0 parameters and method overloads

In what situations in C # 5.0 would you use one above the other: optional parameters and method overloads?

+4
source share
6 answers

if the internal implementation of the function is quite different, then use the overload method. Otherwise, use the optional parameters.

Additional parameter methods allow you to write one test method instead of writing a test method for each overloaded method

+5
source

Well, optional parameters should be used when you want to execute a method, and not indicate to the callers all the parameters.

eg.

private void method (int a, int b = 0){}// if B is optional 

Method overloading should be used when you change parameter types 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 that they are not either / or. They both have different goals.

+1
source

I find that when I add a switch to an already simple method, I would use an optional parameter like

 public List<Stuff> GetItems (Guid stuffID, bool includeDeleted = false) { //implementation } 

The advantage is that the GetItems method could already be widely used in other areas of the application, and you do not want you to add and add your own parameters for each use.

Optional parameters become a problem when you have a method with a lot of parameters and control that is optional and that doesn't start working. In this case, you need to start specifying the parameters that you provide. I find it a bit of a mess

Anything more complicated than the above example should really use overloads. I cannot come up with any example where I would use more than one optional parameter.

+1
source

As indicated, optional parameters should be at the end of the list. But for the most part, it doesn't matter.

There are indications that the only reason many of the .NET classes use overloads is because additional options are not available when these classes were written. Therefore, I usually find additional parameters that are slightly easier to write than several versions of my methods.

But, again, I really don't see any right or wrong way here.

0
source

Method overloads can be replaced with optional parameters in several cases, but they are not exactly 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, everything is fine, I would prefer additional parameters, because it makes the code shorter and more understandable.

0
source

I think that it doesn’t matter using optional parameters instead of overloading, but I have to say that I ran into a problem using optional parameters due to their “constant limitation”. 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 could not do this because at compile time I got

"optional parameters must be a compile-time constant

So, you should keep in mind that when overloaded, you can get around problems like this by using two different signatures and optionally initializing the parameter.

0
source

Source: https://habr.com/ru/post/1445089/


All Articles