Click here to Skip to main content
11,493,937 members (70,193 online)
Click here to Skip to main content

An Introduction to Named Parameters - C#4.0

, 27 Oct 2010 CPOL 14.5K 58 6
Rate this:
Please Sign up or sign in to vote.
TThis article will tell about the advantage of using named parameter

Introduction

C#4.0 has bring the new Named Parameter in its collection to give developers the flexibility of applying the parameters at their own discretion.

Background:

Consider the below example which is done in in a lower version of .Net

class Program
    {
        static void Main(string[] args)
        {
            WithoutNamedParameter(10, "SomeName");
            WithoutNamedParameter("SomeName",10);
            Console.ReadKey(true);
        }

        private static void WithoutNamedParameter(string name, int age)
        {
            Console.WriteLine(string.Format("{0},your age is {1}", name, age));
        }

        private static void WithoutNamedParameter(int age, string name)
        {
            Console.WriteLine(string.Format("{0},your age is {1}",name,age));
        }
    }

As can be seen that the function WithoutNamedParameter is a simple overloaded method where it accepts only 2 parameter and that too of same type (int and string) with the only difference that the order is changed. Though the output will be same in both the case.

This happens in real time scenario and the developer had no option but to create a overloaded method.

C#4.0 has culminated the problem by the introduction of Named parameter where the developers has the flexibility of using the parameters.

Overloaded problem solved using Named Parameter

Now let us see how C#4.0 has overcome such situation. Consider the below program

class Program
    {
        static void Main(string[] args)
        {
            #region Named Parameter Example
            WithNamedParameter(Age: 10, Name: "SomeName");
            WithNamedParameter(Name: "SomeName", Age: 10);
            Console.ReadKey(true);
            #endregion
        }

        private static void WithNamedParameter(string Name, int Age)
        {
            Console.WriteLine(string.Format("{0}, your age is {1}", Name, Age));
        }
    }

As can be seen that in the WithNamedParameter it is just the Parameter name that we are specifying in the method call followed by a colon( : ) and then the parameter value while the compiler does the rest. Is not that handy?

The output is as expected

3.jpg

Conclusion:

In this short article we have seen how named parameter help developers to avoid writing overloaded methods and rather helps in code reusability.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 1 Pin
Batzen28-Oct-10 2:52
memberBatzen28-Oct-10 2:52 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 10:18
memberSledgeHammer0127-Oct-10 10:18 
GeneralMy vote of 1 Pin
Argyle4Ever27-Oct-10 5:28
memberArgyle4Ever27-Oct-10 5:28 
GeneralMy vote of 2 Pin
Toli Cuturicu27-Oct-10 3:49
memberToli Cuturicu27-Oct-10 3:49 
GeneralMy vote of 1 Pin
Keith Barrow26-Oct-10 8:05
memberKeith Barrow26-Oct-10 8:05 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 5:46
memberSelvin26-Oct-10 5:46 
GeneralGood Pin
Nandakumar Rangaswamy26-Oct-10 3:19
memberNandakumar Rangaswamy26-Oct-10 3:19 
GeneralMy vote of 4 Pin
luisnike1925-Oct-10 19:55
memberluisnike1925-Oct-10 19:55 
GeneralRe: My vote of 4 Pin
Niladri_Biswas26-Oct-10 1:21
memberNiladri_Biswas26-Oct-10 1:21 
GeneralRe: My vote of 4 Pin
ThatsAlok 26-Oct-10 2:45
member ThatsAlok 26-Oct-10 2:45 
GeneralMy vote of 1 Pin
Sacha Barber25-Oct-10 6:19
mvpSacha Barber25-Oct-10 6:19 
GeneralRe: My vote of 1 Pin
Niladri_Biswas25-Oct-10 6:47
memberNiladri_Biswas25-Oct-10 6:47 
GeneralYou are always well come Pin
Khaniya27-Oct-10 20:47
memberKhaniya27-Oct-10 20:47 
GeneralRe: You are always well come Pin
Toli Cuturicu28-Oct-10 3:01
memberToli Cuturicu28-Oct-10 3:01 
GeneralRe: You are always well come Pin
Niladri_Biswas28-Oct-10 5:28
memberNiladri_Biswas28-Oct-10 5:28 
GeneralRe: You are always well come Pin
Niladri_Biswas28-Oct-10 5:25
memberNiladri_Biswas28-Oct-10 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid