C# and .NET interview question: - Define Named and Optional Arguments?
- By Shiv Prasad Koirala in C#
- Aug 11th, 2011
- 911
- 0
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.
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
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more
- By Shiv Prasad Koirala
- Jan 17th, 2012
- 13428
- 0
.NET interview questions: - Can you elaborate project life cycle?
- By Shiv Prasad Koirala
- Aug 3rd, 2011
- 10195
- 0
Five most important SharePoint 2010 interview questions
- By Shiv Prasad Koirala
- Nov 16th, 2011
- 9924
- 0

