What is Named Parameters

Named parameters differs from a regular function call in that the values are passed by associating each one with a parameter name, instead of providing an ordered list of values. You can use the named parameters from in C# from 4.0. Besides C#, Ada, Common Lisp, Fortran, Mathematica, Objective-C, PL/SQL, Perl, Python, R, Scala, Smalltalk, Visual Basic are all support the named parameters.

A Demo of Named Parameters

For example, we have a class named Person with a constructor which will received fistName, lastName, and age.

   1: public class Person

   2: {

   3:     public Person(string firstName, string lastName, int age)

   4:     {

   5:         this.FirstName = firstName;

   6:         this.LastName = LastName;

   7:         this.Age = age;

   8:     }

   9:  

  10:     public string FirstName { get; set; }

  11:     public string LastName { get; set; }

  12:     public int Age { get; set; }

  13: }

Using the named parameters, we do not need to call the instructor one by one, but give value with parameters. like this

var person1 = new Person(firstName: "Fx", lastName: "Jack", age: 20);var person2 = new Person(lastName: "Jack", age: 20, firstName: "Fx");var person3 = new Person(lastName: "Jack", firstName: "Fx", age: 20);

and the person1, person2, and person3 are the same indeed.

Benefit of using named parameters

  • makes the code more understandable. 
  • provide the flexibility plus the ‘optional parameters’
  • it can make your code higher quality.