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.
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.
string Name="Vithal Wadje";
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.
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.
- 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.
- "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.
- 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.