1. Back To Blog

C# and .NET interview question: - Define Named and Optional Arguments?

C# and .NET interview question: - Define Named and Optional Arguments?

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.



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.




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.




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




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.




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




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

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social