Named parameters in C#

Introduction

In this article, I will discuss the named parameter. Nowadays in most of the interviews, you will get this question.

Named parameters

When we are passing some parameters to methods that time we have to remember the parameter orders, because if the parameter execution order will not the same as the passed sequence, then it will cause the error. In named parameters, we do not have to remember the sequence of the parameter. Here we just have to mention the name of the parameter, then when we will call the parameter it will find the parameter as per the name not by the order of the sequence.
Here, follow the code to know about how to implement the named parameter
 public static void NamedParameter(int i, string firstName, string lastName)
        {
            string strResult = firstName + i + lastName;
            Console.WriteLine("Final value is: " + strResult);
        }
static public void Main()
 {
  NamedParameter(lastName: "Mishra", i: 2, firstName: "Bidyasagar");
 }
OUTPUT: Final value is: Bidyasagar2Mishra
  • In the above example, parameter sequences are not matching between the calling method and called method.
  • Here, I did not find any error due to the use of the named parameter

Summary

In the above article I discussed the named parameter in c#, I hope, you got the proper implementation of the named parameter.  If you find helpful information from this article, please share it with your colleagues and the development community. Happy to help you 😊.

Leave a Comment

Your email address will not be published. Required fields are marked *