find and solve || findandsolve.com
Please wait.....

How to use named parameters in C#

In the C#, Named Parameters allow developers to pass a method argument with parameter names which introduce c# 4.0. Name introduced to pass the method arguments with parameter names rather than with the method parameter’s position in the parameter list.


Generally, while calling the method you need to pass all the method arguments in the same sequence of parameters in the method definition. If you use named parameters, you don’t need to worry about the order or sequence of parameters while calling the method.


In named parameters, the parameter values will map to the right parameter based on the parameter name. So, while calling the methods, you can send the parameters in any sequence or order to specify the parameter name.


For example, you have a Details() method with three parameters (id, name, and address) like as given below.

public static void Details(int id, string name, string address)
{
//put your code here
}


In the standard way, you will call the above method by sending the arguments in the same order or position; however, you defined the parameters in the method definition as given below.


Details(1010, "Sylvia", "Kathmandu");


If you forgot and sent the parameters in your method in a different order as given below, you will get the compile-time error like “The best overloaded method for 'Details(int, string, string)' has some invalid arguments”.


Details("Rahul", 102, "Pokhara");


Using named parameters, you call the above method by sending the parameters in any sequence or order, as given below.


Details(id: 101, name: "Sylvia", address: "Kathmandu");

Details(name: "Rahul", id: 102, address: "Pokhara");
Details(location: "Saru", name: "Dhading", id: 103);


You can also use the named arguments with positional arguments as long as they are not followed by any positional arguments like as given below.


Details(101, "Sylvia", address: "Kathmandu");


The following invalid way of using named and positional arguments while calling the method.


Details(name: "Rahul", 102, address: "Pokhara");


The given above method call will throw the compile-time exception like “Named argument 'name' is used out-of-position but is followed by an unnamed argument” because we defined the positional argument followed by named argument (name).


C# Named Parameters Example


using System;
namespace FindAndSolve
{
   public class Program
   {
      public static void Main(string[] args)
      {
         Details(101, "Sylvia", "Kathmandu");
         Details(name: "Rahul", id: 102, address: "Pokhara");
         Details(103, "Dipa", address: "Mirmee");
         Console.ReadLine();
      }
      public static void Details(int id, string name, string address)
      {
         Console.WriteLine("Id:{0}", id);
         Console.WriteLine("Name:{0}", name);
         Console.WriteLine("Address:{0}", address);
      }
   }
}


Output

Id:101

Name:Sylvia

Address:Kathmandu

Id:102

Name:Rahul

Address:Pokhara

Id:103

Name:Dipa

Address:Mirmee


C# Named Parameters Overview

  • In c# 4.0 The named parameters have been introduced to pass the method arguments with parameter name rather than the method parameter’s position in the parameter list.
  • In named parameters, the parameter values will map to the right parameter based on the parameter name.
  • From c# 4.0 onwards, you can send the parameters in any sequence or order to specify the parameter's name while calling the methods.
  • While calling the method, you can use the named arguments with positional arguments as long as they are not followed by any positional arguments.