Home »
.Net »
C# Programs
C# program to demonstrate the named arguments
Here, we are going to demonstrate the named arguments in C#?
Submitted by Nidhi, on November 08, 2020
Here, we will demonstrate the named arguments, using named arguments, we can pass argument in the method without predefined order.
Program:
The source code to demonstrate the named arguments is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to demonstrate the named arguments.
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 »