You are on page 1of 9

5/16/2017 OptionalandNamedParametersinC#

In Focus
C# Corner
DotNetDave Rocked The C# Corner Conference
Optional and Named
Ask aParameters
Question inContribute
C#

Vithal Wadje Oct 27 2013 Article

1 4 16.4k

UsingNamedandOptionalParameter.zip
Download 100% FREE Spire Oce APIs

Background

In prior versions of the C# Language it is necessary to the number of parameters declared in a method when the method is
called, in other words it's not optional to not all the parameters of that method. Also they must be ed 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 parameters in any sequence and also there is no need to 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 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.

For example:

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 onthat type default value.

For example:

int a=1000;
string Name="Vithal Wadje";

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 1/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
3. Optional Parameters must be declared as the last parameters with default values for the method after all the
C# Corner
DotNetDave Rocked
required The C# Corner Conference
parametes.

Forexample: Ask a Question Contribute



Public void DetailsString Name,String City,String State="";//with Null
Public void DetailsString 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 parametes including state that are optional
parameters and when calling the GetCustmorDetails method we are just ing two parametes because state is an optional
parameter and there is no need to 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 ed or not ed 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 ing 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 ing parameters to a method.

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 2/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
2. When calling a method, parameters are ed with the Parameter Name of the method.
C# Corner
DotNetDave Rocked The C# Corner Conference
Forexample:

Ask a Question Contribute
Public void DetailsString Name,String City,String State; //method
Public void Details State:"Maharashtra" ,Name: "Vithal Wadje",City:"Latur";//ing parameters while calling method
with Named Parameters

Let us see the following example to learn in detail.

In the preceding example we are ing the parameters to a method using the C# 4.0 new feature Named
Parameters.

Problems Solved

In prior versions it is very dicult to remember the sequence of parameter when ing parameters to a
method but in C# 4.0 you can 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 rightclick on Solution Explorerand select"Add New Item" "Default.aspx" page and one class file.
5. Drag and drop three Text Boxes and two buttons.

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 3/9
5/16/2017 OptionalandNamedParametersinC#

InThen
Focus
the source section of the Default.aspx page looks as in the following:
C# Corner
DotNetDave Rocked The C# Corner Conference

Ask a Question Contribute

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 le on button click as in the following:

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 4/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
C# Corner
DotNetDave
Now run theRocked The C#
application Corner
and enter Conference
values for Name and City in the text box and click on the optional button
as in the following:
Ask a Question Contribute

In the preceding example we are ing a name and city to "GetCustmorDetails" and we are not ing 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:

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 5/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
C# Corner
DotNetDave Rocked
The preceding The C#
examples Corner
show thatConference
the optional parameters may be ed or may not be ed, in other words it is
not mandatory to them when calling the method.
Ask a Question Contribute
Now Enter on the Named button with the same values as above and when ing 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

For detailed code please download the Zip file attached above.
A video tutorial of this article will soon be uploaded to the C# Corner video section.

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.

Download 100% FREE Spire Oce APIs

C# Named and Optional parameter C# Parameter Named parameter in C#

Vithal Wadje

Vithal is a Developer , Software Architect, Microsoft MVP (2016) , C# Corner MVP (5 times) ,
Microsoft C# TechNet Guru (March 2015) , Author , Blogger ,Speaker , C# Corner Mumbai
User Group Lead and Student's Mentor ... Read more

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 6/9
5/16/2017 OptionalandNamedParametersinC#

In Focus http://www.compilemode.com
C# Corner
DotNetDave Rocked The C# Corner Conference
9 16.5m 5 1
Ask a Question Contribute
1 4

Type your comment here and press Enter Key (Minimum 18 characters)

thanks shivanand sir


Vithal Wadje Feb 17, 2014
9 46k 16.5m 1 0 Reply

U are doing good Vithal :)


Shivanand Arur Feb 12, 2014
350 3.6k 592.7k 0 0 Reply

thanks Ravi
Vithal Wadje Nov 28, 2013
9 46k 16.5m 0 0 Reply

Nice article sir. We use this optional parameter in vb.net in earlier version
Ravi Shekhar Nov 28, 2013
292 4.5k 754.5k 0 0 Reply

Comment Using

0Comments Sortby Oldest

Addacomment...

FacebookCommentsPlugin

File APIs for .NET


Aspose are the market leader of .NET APIs for le business formats natively work with
DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

TRENDING UP

01 Tuples In C# 7 - What Are They And How Can I Use Them?

02 Local Functions In C# 7.0

03 Important Tips To Write Clean Code In C#

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 7/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
04 Developers
C# Corner
DotNetDave
Use Case For .NET API Browser
Rocked The C# Corner Conference

05 Getting Started With Angular 2 Using Visual Studio Code


Ask a Question Contribute
06 Entity Framework 6.0.0 Using Stored Procedure In ASP.NET MVC

07 What You Need To Know About Visual Studio 2017 In A Nutshell

08 Who A Microsoft Regional Director Is

09 SPA Using Angular 2, ASP.NET Core 1.1 And Entity Framework Core - Part Two

10 UI-Grid With AngularJS And WebAPI


View All

Follow@csharpcorner 91.7Kfollowers

C#Corner
1,284,465likes

Liked LearnMore

Youand6otherfriendslikethis

Philadelphia
New York
London
Delhi

Join C# Corner
A community of 2.3 million developers worldwide.

Enter your email address Sign Up

Learn ASP.NET MVC Learn ASP.NET Core Learn Python Learn JavaScript Learn Xamarin
Learn Oracle More...
Home Events Consultants Jobs Career Advice Stories

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 8/9
5/16/2017 OptionalandNamedParametersinC#

In Focus
C# Corner
DotNetDave Rocked The C# Corner Conference

Ask a Question Contribute


About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ
2017 C# Corner. All contents are copyright of their authors.

http://www.csharpcorner.com/UploadFile/0c1bb2/namedandoptinalparameterinCSharp/ 9/9