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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 212
    Comment on it

    Named parameters:-

     

           The concept of named parameters was introduced in C#4.0 and main purpose was to pass the parameter by name not the position. This features provide us the liberty to pass the parameter at any position without bothering its position in parameter list and code looks more clear and readable.

    Example:-

     

     public int CalculateArea(int width, int height)
     {
                // Calculate are
     }

     

    I have method named CalculateArea which calculates the area based on width and height. If we have to call this function we will have to provide the width as the first argument and height as the second argument otherwise result might not be correct but named parameters provides us the liberty to pass the parameter at any order just by specifying the name of the parameter. please see below code how to call the function by named parameters

     

       CalculateArea(10, 20);
       CalculateArea(width: 10, height: 20);
       CalculateArea(height: 20, width: 10);

     

    In above three lines of code I have used 3 types of calling conventions. In the first line of code I have used conventional way of calling the function by passing the values in ordered way, In second line code I have used the named parameters by specifying the name of parameter for which the values is being passed and in third line of code I have changed the order of parameters which would not impact the result. All of above call would return the same result.

     

 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: