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.