C# and .NET interview question: – Show with example Named and Optional Arguments in C# 2010?

Visual C# 2010 introduces two interesting features called as Named and Optional Arguments.

Named Argument: – Named Argumentallows you to explicitly name an argument you are passing to a method, constructors and properties with indexes – instead of just identifying it by argument position.

For example: – suppose we have created a method named as “Multiply” with two parameters namely “i” and “j”, just like below code snippet.

staticint Multiply(int i = 0, int j = 0)
{
return i * j;
}

Now, let’s see how we can call the above method and pass the value to the parameters using “Named Argument”.

You can call the method in one of the following ways: –

//Note that the position of parameters doesn’t matters while passing the values.

Console.WriteLine(Multiply(i: 10));

Console.WriteLine(Multiply(j : 5));

Console.WriteLine(Multiply(i : 10 , j : 5));

Console.WriteLine(Multiply(j : 5 , i : 10));

Because both parameters are optional, in cases where only one (or zero) parameters are specified then the default value for any non-specified arguments is passed.

Optional Argument: -Optional arguments enable you to omit arguments for some parameters.

Parameters are optional when a default value is specified to a particular argument.

For Example: -suppose we have created a method named as “Multiply” with two parameters namely “i” and “j” and have set the value j = 5, just like below code snippet.

staticint Multiply(int i = 0 , int j = 5)
{
return i * j;
}

Now, let’s see how we can call the above method and pass the value to the parameters using “Optional Argument”.

Note that VS 2010’s Intellisense indicates when a parameter is optional, as well as what its default value is when statement completion is displayed: –

In the above diagram you can see that the VS 2010 Intellisense indicates the parameter in square bracket which is optional with its default value.

Now, let’s demonstrate a small example on both the features to get a better idea.

Let first demonstrate an example for “Named Argument”.

Step1: – create a new project go to File > New > Project > Windows > Console Application.


Step2: – Now, just create a method with two parameters like below code snippet.

staticint Add(int i , int j)
{
return i + j;
}

Just call the method in the main class like code snippet.

staticvoid Main(string[] args)
{
Console.WriteLine("*****Begin Test*****");
Console.WriteLine("");
Console.WriteLine("Example of Named Arguments");
Console.WriteLine("");
//Here the Named Argument has been Used.
Console.WriteLine("The Addition of two Numbers:"+ Add(j : 2 , i : 10));
Console.ReadLine(); 
}

The below diagram will show how exactly the VS 2010 Intellicense act while passing the value to the method parameters.

Now, let’s demonstrate an example for “Optional Argument”.

Step1:- Follow the same as we have done for the “Named Argument”.

Step2: – Create a method with two parameter namely “i” and “j” and also set default value to j = 5 like below code snippet.

staticint Add(int i , int j = 5)
{
return i + j;
}

Just call the method in the main class like code snippet.

staticvoid Main(string[] args)
{
Console.WriteLine("*****Begin Test*****");
Console.WriteLine("");
Console.WriteLine("Example of Optional Paramater Arguments");
Console.WriteLine("");
//In the above code i have just passed the value for i only but 
the value for j will be 5 by default.
Console.WriteLine("The Addtion of two Numbers:" + Add(10)); 
Console.ReadLine(); 
}

The below diagram will show how exactly the VS 2010 Intellicense act while passing the value to the method parameters.

Related to interview, view the following video on mock of C# and .NET interview


Get more tutorials for C# interview questions and answers

Regards,

Also view author’s blog for Most asked c# interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s