.Net Interview Questions and Answers

Search:

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

By : Shiv Prasad Koirala | Aug 11th, 2011 | Views : 1268 | Comments : 0 Bookmark and Share

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

 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
Comments

Write a Comment

All fields marked with * are mandatory

310f3

 

 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

WCF Interview questions and answers: -What is the difference between WCF fault exceptions and normal .NET exceptions?

In this article we will show difference between WCF fault exceptions and normal .NET exceptions. For more articles and videos visit us on http://www.questpond.com/... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

MVC vs ASP.NET ( MVC Interview question with answers )?

In this article we will explain MVC vs ASP.NET.For more articles and videos visit us on www.questpond.com... Read More

.NET interview questions: - Can you elaborate project life cycle?

In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

WCF Interview questions and answers: -What is the difference between WCF fault exceptions and normal .NET exceptions?

In this article we will show difference between WCF fault exceptions and normal .NET exceptions. For more articles and videos visit us on http://www.questpond.com/... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

MVC vs ASP.NET ( MVC Interview question with answers )?

In this article we will explain MVC vs ASP.NET.For more articles and videos visit us on www.questpond.com... Read More

.NET interview questions: - Can you elaborate project life cycle?

In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More

Article Categories