Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is optional paramter in c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: