Optional and Named Parameters in C#

In prior versions of the C# Language it is necessary to pass the number of parameters declared in a method when the method is called, in other words it's not optional to not pass all the parameters of that method. Also they must be passed in the sequence they are declared for the method. In C# 4.0 however the feature "Named and Optional Arguments" has been added by which you can pass parameters in any sequence and also there is no need to pass all the parameters for that method.
So let us learn about it from the basics so beginners can also understand it.
What Optional Parameters are
An optional parameter is a parameter in a method declared with some default value. Because of the default value when we call the method there is no need to pass the parameter or value  for that method when calling.
Key Points


     1. An Optional Parameter must be assigned some value such as Null if it is a string and zero if it is an  integer or something else depending on the type of the default value.  
   int a=0;
   string Name="";

      2. An Optional Parameter also allows the assignment of a value of that type instead of Null if it is  string  and zero if it is an integer or any other depending on that type default value 
    int a=1000;
    string Name="Vithal Wadje";

   3. Optional Parameters must be declared as the last parameters with default values for the method after all the required parameter
e.g
Public void Details(String Name,String City,String State="");//with Null
Public void Details(String Name,String City,String State="Maharashtra");//with string
Let us see the following example to learn the details
Optional Parameter
In the preceding example the GetCustmorDetails method takes the three parameters including state that are optional parameters and when calling the GetCustmorDetails method we are just passing two parameters because state is an optional parameter and there is no need to pass the value for that parameter in the call of the method.
Problems Solved
It solves the problem of, when in my method there are many parameters and one of them is optional, in other words it may be passed or not passed in the call of the method. An optional parameter is useful and depending on your requirements you can use it for any purpose.
Later on we will see a realistic example of an optional parameter but let us see first about Named Parameters in C# 4.0.
What Named Parameters are in C# 4.0
Named Parameters
allows the passing of parameters to a method in any sequence instead of depending on the parameter's position as declared in the method.
Key points

  1. There is no need to follow the sequence when passing parameters to a method.
  2. When calling a method, parameters are passed with the Parameter Name of the method.
    For example:

    Public void Details(String Name,String City,String State); //method
    Public void Details( State:"Maharashtra" ,Name: "Vithal Wadje",City:"Latur");//passing parameters while calling method with Named Parameters 
    Let us see the following example to learn in detail.
Named parameters


In the preceding example we are passing the parameters to a method using the C# 4.0 new feature Named Parameters.
Problems Solved
In prior versions it is very difficult to remember the sequence of parameter when passing parameters to a method but in C# 4.0 you can pass it in any sequence and depending on your requirements you can use it for any purpose.
Now let us see the realistic example of optional and named parameters.
Create the web application as in the following:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Project..." then select "C#" - "Empty web site" (to avoid adding a master page).
  3. Provide the project a name, such as "NamedandOptionalParam" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page and one class file.
  5. Drag and drop three Text Boxes and two buttons
Then the source section of the Default.aspx page looks as in the following: ASPX code
Now add a class by right-clicking on Solution Explorer, name it Customer.cs and add the following method.
 CS code
Now call the GetCustmorDetails method in the default.aspx.cs file on button click as in the following:
CS code on button click
Now run the application and enter values for Name and City in the text box and click on the optional button as in the following:
optional 
In the preceding example we are passing a name and city to "GetCustmorDetails" and we are not passing a state because it is optional for the method.
Now enter input in all three text boxes and click on the same optional button; the output will be as in the following:
optional output
The preceding examples show that the optional parameters may be passed or may not be passed, in other words it is not mandatory to pass them when calling the method.
Now Enter on the Named button with the same values as above and when passing theses values on a button click we are not following any parameter sequence but the output will be the same as we expected as in the following:
parameter
 From all the examples above, I hope you understand the Named and Optional Arguments feature of C# 4.0.
Note
  • A video tutorial of this article will soon be uploaded to YouTube.
Summary
We learned about the Named and Optional Arguments feature of C# 4.0, I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode