Monday 23 October 2017

Named Parameters in C#.

In this article we will discuss about How to use Named Parameters in C#.


By using Named Parameters in C#, We can Pass Value to Parameter in any Sequency or Order without worrying about any data.



For Example:-

------------------------


First let me take an example without Using Named Parameters and later on i will tell you the problem and How to resolve it.



Let's take an method,



private void NamedParamEx(string Fname,string Lname)

{
// Our rest Of Code
}


while calling this method we need to pass 2 Parameters as Fname and Lname which is as below,



NamedParamEx("James","Wrink");

the above Calling looks good but what if we Swaps the 2 values then everthing goes Wrong.


To OverCome that Problem, Named Parameters Came into picture and example is as below,


NamedParamEx(Fname:"James",Lname:"Wrink");


or



NamedParamEx(Lname:"Wrink",Fname:"James");



The above 2 example works fine in both the Scenarios and it is better to Use Named Parameters because to avoid misplacing of data.

0 comments:

Post a Comment