blogs

miércoles, 10 de junio de 2015

Optional Argument and Name Argument

An argument is data passed to a method's parameter when it is invoked
When calling methods, we must supply arguments that match the parameter.

Optional Argument or parameter Default values:

A optional argument :

  • Allow developers to supply typical values.
  • Allow developers to hint at usage
  • Reduce the number of method overrides
  • Allow parameters to be optional to the caller.

For example

we have a  method:
class Customer{
public  int AddPoints(int pointToAdd=1)
{
    point=pointToAdd;
}
}
This parameter is optional, if we don't put the parameter, the values by default is 1.

For Example.

We will use the transport's project

Create a test method and create an instance class.

Run test



Execution on line 13




Add new parameter and property in class Car to test the order the parameters




Optional argument forces us to put in order the parameters


 Method execution





the problem is when we want to put just some parameters, we must put them sequentially.

For this reason exists other way to do this.

Name argument:

Name argument
  • Allow developers to specify argument in an order
  • Allow developers to easily handle optional arguments
  • Positional and named arguments can be mixed.

Create a test method







 Run test






No hay comentarios:

Publicar un comentario