Optional Parameter and its uses [Resolved]

Posted by Amatya under C# on 2/14/2016 | Points: 10 | Views : 432 | Status : [Member] | Replies : 5
I was asked Optional Parameter in 2+ year experience, Interview round. I was unable to convince him. Can anyone explain and define it, so i can easily explain in face to face interview round.

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks



Responses

Posted by: Rajnilari2015 on: 2/15/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Amatya Sir,

C#4.0 has bring the new Optional Parameter in its collection to give developers the flexibility passing parameters at their discretion. Consider the below example which is done in C#3.0

class Program
{
static void Main(string[] args)
{
WithoutOptionalParameter(null,null); //Print default values
WithoutOptionalParameter("Some Name", null); //Print only default age
WithoutOptionalParameter(null, 25); //Print only default name
WithoutOptionalParameter("Some Name", 50); //Print the value passed

Console.ReadKey(true);
}

private static void WithoutOptionalParameter(string name, int? age)
{
string _name = "Default Name";
int? _age = 18;

//Check if name is empty or not and hence assign
if (!string.IsNullOrEmpty(name)) _name = name;

//Check if age is empty or not and hence assign
if (age.HasValue) _age = age;

Console.WriteLine(string.Format("{0},your age is {1}", _name, _age));
}
}


In the WithoutOptionalParameter method first we have assigned two default values to the two variables _name and age. Then we are checking, if the Calling function has assigned any value to the method parameters or not and depending on that, we are displaying the values.

Next have a look at the calling function. The method WithoutOptionalParameter has been called four times with four different values. But the code looks pretty ugly.

Another option was to create four overloaded method to satisfy the requirement if the Empty or Null checking needs to be avoided which on the other hand was again a tedious job.

The new Optional Parameter, introduced with C#4.0 has a better look.

Consider the below program written in C#4.0 with Optional Parameter

class Program
{
static void Main(string[] args)
{
MethodWithOptionalParameters();//Print default values

MethodWithOptionalParameters("Some Name");//Print only default age

MethodWithOptionalParameters(Age: 25);//Ask to print only Name

//Prints the values passed
MethodWithOptionalParameters("Some Other Name", 20);

Console.ReadKey(true);
}

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


As can be observed that first of all in the new code we are no longer checking the Empty/Null values of the method parameters. Instead , we have assigned the default values to the method parameters.

Consider the First Calling Method. We are not at all passing any parameter. The compiler will happily compile though (which was not the case in the earlier example as it would have reported the error No overload for method 'WithoutOptionalParameter' takes '0' arguments.

Consider the third Method call

MethodWithOptionalParameters(Age: 25);//Ask to print only Name

Here we are specifying the Age only which is a Named Parameter in this case and the compiler will understand that and will do the favor.

In short, optional parameter help developers to avoid writing overloaded methods and rather helps in code re usability.

Hope this helps.

N.B.~
The answer presented here is an excerpt from my original article http://www.codeproject.com/Articles/121496/An-introduction-to-Optional-Parameters-C?msg=3646311#xx3646311xx

--
Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 2/16/2016 [Member] Silver | Points: 25

Up
0
Down
Thanks bro.. I got clear @RajniLari

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 2/16/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Amatya , glad that it helped you.

--
Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 2/17/2016 [Member] Silver | Points: 25

Up
0
Down
Really nice .. I start using in my application .. :)

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 2/17/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Amatya , glad to know that

--
Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response