SIGN UP MEMBER LOGIN:    
ARTICLE

Named arguments in C# 4.0

Posted by Shankey Articles | .NET 4.5 December 02, 2010
This new feature of c# 4.0 allows users to define arguments in the calling statement in any order rather than defined in the parameter list.
Reader Level:
Download Files:
 

This new feature of c# 4.0 allows users to define arguments in the calling statement in any order rather than defined in the parameter list (Argument list references the calling statement and parameter list references the method prototype).  With this new feature the user can call a method by passing arguments irrespective of their actual position. Thus the users are free from the headache of remembering the position of the parameters while calling a method:

//calculates the n to the power of x
static double CalculatePower(double x, double n)
{
    return Math.Pow(x,n);
}

User calls this method in following:
  1. Following image depicts the calling way and the order of the assignment of the values to the parameter

    1.gif

  2. Following image depicts the second calling way and the order of the assignment of the values to the parameter

    2.gif

  3. Following image depicts the third calling way and the order of the assignment of the values to the parameter

    3.gif

Following is the output:

4.gif 

Let us elaborate the output

When user calls the function using following line the x will get 2 as its value and n will get 4 as its value

double result=  CalculatePower(2,4);

and following is the result printed because of 24

Result is :16

When user calls the function using following line the x will get 2 as its value and n will get 4 as its value

result = CalculatePower(x:2,n:4);

and following is the result printed because of 24

Result is :16

When user calls the function using following line the x will get 5 as its value and n will get 2 as its value

result = CalculatePower(5,2);

and following is the result printed because of 52

Result is :25

When user calls the function using following line the x will get 2 as its value and n will get 5 as its value

result = CalculatePower(n:5, x: 2);

and following is the result printed because of 25

Result is :32

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor