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.
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="";
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
Now add a class by right-clicking on Solution Explorer, name it Customer.cs and add the following method.
Now call the GetCustmorDetails method in the default.aspx.cs file on button click as in the following:
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:
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:
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:
From all the examples above, I hope you understand the Named and Optional Arguments feature of C# 4.0.
Note
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.
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";
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
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
- There is no need to follow the sequence when passing parameters to a method.
- 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); //methodPublic void Details( State:"Maharashtra" ,Name: "Vithal Wadje",City:"Latur");//passing parameters while calling method with Named ParametersLet us see the following example to learn in detail.
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:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New" - "Project..." then select "C#" - "Empty web site" (to avoid adding a master page).
- Provide the project a name, such as "NamedandOptionalParam" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page and one class file.
- Drag and drop three Text Boxes and two buttons
Now add a class by right-clicking on Solution Explorer, name it Customer.cs and add the following method.
Now call the GetCustmorDetails method in the default.aspx.cs file on button click as in the following:
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:
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:
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:
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.
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