The concept of optional parameter was introduced in C#4.0. Optional parameters enables you to pass the only required parameters. Each optional parameter has its default value, if no value is passed for this default parameter then its default value would be used. Default value must be of constant type and it must be defined at the end of the parameter list and it can be applied on indexer, method and constructor.
Syntax of optional parameter:
In above image empName , age are required parameters while location is optional parameters.
if we don't provide the value of 3rd parameter then location would used its default value which is "India".
Calling conventions:
In the above code we can see that location is not provided while saving data for "Amit" (Line no-1) in this case default value of location parameter would be used which is "India" but in second call we are proving the location "USA" which will overwrite the value of default parameter.
0 Comment(s)