C# - Named Arguments Example

Here, we will learn about the named arguments in C# with the help of example.
Submitted by Nidhi, on November 08, 2020 [Last updated : March 23, 2023]

Named Arguments

Using the named arguments, we can pass argument in the method without predefined order. Here, we will demonstrate the example of named arguments.

C# program to demonstrate the example of named arguments

The source code to demonstrate the named arguments is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - Named Arguments Example.

using System;

class Employee
{
    static public void PrintEmployee(int id, string name, int salary, string department)
    {
        Console.WriteLine("Employee Details:");
        Console.WriteLine("\tEmployee Id        : " + id          );
        Console.WriteLine("\tEmployee Name      : " + name        );
        Console.WriteLine("\tEmployee Salary    : " + salary      );
        Console.WriteLine("\tEmployee Department: " + department  );
    }

 
    static public void Main()
    {
        PrintEmployee(101, "RAHUL", 10000, "HR"     );
        PrintEmployee(102, "ROHIT", 12000, "Sales"  );
        PrintEmployee(id:103,salary:15000, name:"VIRAT", department:"Account");
    }
}

Output

Employee Details:
        Employee Id        : 101
        Employee Name      : RAHUL
        Employee Salary    : 10000
        Employee Department: HR
Employee Details:
        Employee Id        : 102
        Employee Name      : ROHIT
        Employee Salary    : 12000
        Employee Department: Sales
Employee Details:
        Employee Id        : 103
        Employee Name      : VIRAT
        Employee Salary    : 15000
        Employee Department: Account
Press any key to continue . . .

Explanation

In the above program, we created the Employee class that contains two static methods PrintEmployee() and Main().

The PrintEmployee() method is used to print the employee details on the console screen.

In the Main() method, we called the PrintEmployee() methods.

PrintEmployee(id:103,salary:15000, name:"VIRAT", department:"Account");

In the above code, we passed arguments in the method using the named argument without any specific order.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.