Thursday, August 11, 2011

C# and .NET interview question: - What are 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# and Dotnet interview questions 

Regards,

Also view author’s blog for Most asked Dotnet interview questions

No comments: