Today we will see how to use the Optional and named arguments.
The Visual C# 2010 introduced new feature Named arguments and Optional arguments(parameters). As the name Named argument it allows user to specify the argument for a particular parameter in parameter list by associating the parameter name with the argument.While on other hand the Optional argument allows user to omit the argument for some parameters.Let's see the feature of each type:
Optional Arguments (or Parameters):
The definition of a method can specify that its parameters are required(mandatory) and optional parameters.Therefore any method call must provide required argument and optional arguments can be omitted. Each optional parameter should be provided with default value as a part of its definition.So that no need to pass the optional arguments when we call such method until and unless you have the optional parameter value other than the default value.
**The default value must be one of the following type:
1.Value type
2.Constant
Examples:
Class Demo
{
public void EnterEmployeeDetails(int intEmpNo,int intDeptCode= 1,string strCity="ABC")
{
//do the required operation here....
}
}
In above example we have given intDeptCode and strCity as optional parameter hence whenever we call this method and dont want to pass the optional parameters then just make method call as below:
Demo objDemo= new Demo();
objDemo.EnterEmployeeDetails(1234);
**Note: Points to remember when using Optional arguments.
1.Optional arguments are defined at the end of parameterlist after the required parameters.
2.Comma separated gaps in the argument list are not allowed the compiler will throw an error.
e.g.
//allowed..
Demo objDemo= new Demo();
objDemo.EnterEmployeeDetails(1234);
//allowed...
Demo objDemo= new Demo();
objDemo.EnterEmployeeDetails(1234,2);
//not allowed...
Demo objDemo= new Demo();
objDemo.EnterEmployeeDetails(1234, ,"XYZ");
If you want to pass only empno and city then it can be done as below:
//here we have specified the optional parameter name and then passed the value.
Demo objDemo= new Demo();
objDemo.EnterEmployeeDetails(1234,strCity:"XYZ");
This is about the optional arguments in brief. Now we will see what is named argument.
Named Arguments:
Named arguments gives you freedom from the need to remember the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name.
Here in our Example CalculateArea(float length,float width) that calculates the area of rectangle.
In regular way we remember the order of the parameters to be passed when making a function call in program viz. first parameter value should be length of rectangle and second parameter value should be width.
E.g. If we have rectangle of length 10 mtrs and width 5 mtrs then we will pass the arguments as below:
CalculateArea(10,5);
However if we know the parameter name then we can pass the argument in any order by using the parameter name.
CalculateArea(length:10 , width:5);
OR
CalculateArea(width:5 , length:10);
**Note: Points to remember when using the Named arguments.
1.A named argument can follow positional arguments as illustrated below:
//allowed...
CalculateArea(10 , width:5);
2.A positional argument cannot follow a named argument- this will throw compilation error.
//not allowed...
CalculateArea(length:10 , 5);
**The main advantage of the Named arguments is it improves code readability.