Introduction: Named Arguments In C#

In this post, we will learn about Named Arguments In C# with an example.

Description:

In my previous posts I have explained various topics like Introduction To JSON, AJAX Basics, Console Application, ASP.NET WEBFORM, ASP.NET MVC.



Now in this post, I will explain about Named Arguments In C# with appropriate example. Normally, In Methods call, arguments are supplied as per the order of parameters defined in the method definition. So, when making a method call, it is important for you to remember the order of parameters before passing the value to the method call. If the number of parameters is large, then it is difficult to remember the order.

To handle this problem, Named arguments are used. When calling a method, arguments are passed with the parameter name followed by a colon and a value. In Named Arguments, we do not need to pass the parameters in order as defined on method definition, so we can pass the arguments in any order on method calling.

Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;

namespace ConsoleDemo
{   
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DotNetK.com");

            Program.EmployeeDetails(Employee_No: 4, EmployeeName: "Vivek", Department: "HR", id: 2);

            // Arguments are passed in different order,which is diffrent from method declaration.
            Program.EmployeeDetails(EmployeeName: "Raj", Employee_No: 44, id: 1, Department: "Finance");
            Console.ReadLine();  
        }
        public static void EmployeeDetails(int Employee_No, string EmployeeName, int id, string Department)
        {
            Console.WriteLine(string.Format("Employee_No : {0}, Name: {1}, id: {2}, Department: {3}", Employee_No, EmployeeName, id, Department));
        }
    }   
}
Named Arguments In C#

Named Arguments In C#

Read :

Summary

I hope you got an idea about Named Arguments In C#.
I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

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

Name *
Email *
Website