Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Named Parameter Feature Of C#.NET 4.0

0.00/5 (No votes)
19 May 2012 1  
Named parameter feature of C#.NET 4.0

Introduction

Named parameter is the feature provided by .NET Framework 4.0 to get us rid from the headache of remembering sequence or order of parameters passed in any method. Here one can specify parameter name with each argument, which makes the programming task easy. This allows one to explicitly name an argument that is being passed, instead of just identifying it by position of argument.

Background

Let's assume your constructor definition looks like this:

public Employee(string firstName, string lastName, DateTime dateOfBirth) 

In that case, one has no other option till framework 3.0 other than to invoke the constructor like this:

Employee employee = new Employee("Shweta", "Jain", new DateTime(1990,1,1)); 

So, now in this case, it is not clear about what is being assigned in the constructor (i.e. that third DateTime parameter might be date of birth, might be date hired, who knows).

Using the Code

In Framework 4.0, one can invoke the constructor like this:

Employee employee = new Employee(firstName: "Shweta", 
lastName: "Jain", dateOfBirth: new DateTime(1990,1,1);   

Points of Interest

This expresses the intent clearly and makes the code more understandable.

To get more interesting information, follow this link.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here