- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are named parameters in C#?
Named parameters provides us the relaxation to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name.
NamedParameterFunction(firstName: "Hello", lastName: "World")
Using named parameters in C#, we can put any parameter in any sequence as long as the name is there. The right parameter value based on their names will be mapped to the right variable. The parameters name must match with the method definition parameter names. Named arguments also improve the readability of our code by identifying what each argument represents.
Example
using System; namespace DemoApplication{ class Demo{ static void Main(string[] args){ NamedParameterFunction("James", "Bond"); NamedParameterFunction(firstName:"Mark", lastName:"Wood"); NamedParameterFunction(lastName: "Federer", firstName: "Roger"); Console.ReadLine(); } public static void NamedParameterFunction(string firstName, string lastName){ Console.WriteLine($"FullName: {firstName} {lastName}"); } } }
Output
The output of the above code is
FullName: James Bond FullName: Mark Wood FullName: Roger Federer
In the above code NamedParameterFunction(lastName: "Federer", firstName: "Roger") even though parameters are not passed in order since we are using named parameters, the parameters are mapped based on the name. So we are getting the output "Roger Federer" which is as expected.
- Related Articles
- What are "named tuples" in Python?
- What are Named Routes in Laravel?
- What are default-parameters for function parameters in JavaScript?
- What are different methods of passing parameters in C#?
- How are parameters passed in C#?
- What are function parameters in JavaScript?
- What are Rest parameters in JavaScript?
- What are Default parameters in JavaScript?
- What are the differences between ref and out parameters in C#?
- What are different types of parameters to a method in C#?
- What are default function parameters in JavaScript?
- What are Rest parameters in JavaScript functions?
- Value parameters vs Reference parameters vs Output Parameters in C#
- What are Named Pipes or FIFO in Linux/Unix systems?
- How are days named?
