How to Use Optional Parameters in C# 4.0

By | | .NET

8

Optional parameters are default values used when a function or method is called without specific arguments. They can be used in PHP…


// PHP optional parameters
function DoSomething($num = 1, $str = "optional")
{
	// code
}

as well as VisualBasic.NET:


' VB.NET optional parameters
Public Sub DoSomething(
	Optional ByVal num As Integer = 1,
	Optional ByVal str As String = "optional"
	)
	' code
End Sub

and similar functionality can be written in JavaScript:


// JavaScript optional parameters
function DoSomething(num, str)
{
	num = (typeof(num) != "undefined" ? num : 1);
	str = (typeof(str) != "undefined" ? str : "optional");
	// code
}

In all cases, calling DoSomething without parameters forces num to be 1 and str to be “optional”. Calling DoSomething with a single parameter will set num, but force str to be “optional”. Calling DoSomething with both parameters overrides your defaults.

Although they can be useful, optional parameters were not implemented in C# until version 4.0. If you’ve recently upgraded, you can now write code such as:


// C# optional parameters
void DoSomething(int num = 1, string str = "optional")
{
	// code
}

Those using earlier versions of C# can still implement optional parameter-like code, but it’s achieved with overloading, e.g.


// no parameters
void DoSomething()
{
	int num = 1;
	string str = "optional";
	// code
}
//
// one parameter
void DoSomething(int num)
{
	string str = "optional";
	// code
}
//
// two parameters
void DoSomething(int num, string str)
{
	// code
}

Overloading can provide a little more control, but it’s a lot more code for an overworked developer to write!

Written By:

Craig Buckler

Craig is a Director of OptimalWorks, a UK consultancy dedicated to building award-winning websites implementing standards, accessibility, SEO, and best-practice techniques.

Website
>> More Posts By Craig Buckler

 

{ 8 comments }

Craig Buckler May 21, 2010 at 5:31 pm

@anon
Unfortunately, your JavaScript code will set the default if a passed argument is falsey (null, false, 0, “”, undefined or NaN).

For example, if you called DoSomething(0, “”), num would be set to 1 and str would be set to “optional” even though you didn’t want that to occur.

anon May 22, 2010 at 9:42 am

good point, thanks for the observation!

anon May 20, 2010 at 12:48 pm

in javascript you can check if a parameter is undefined using the || operator, ie:

function DoSomething(num, str) {
num = num || 1;
str = str || ‘optional’;
// code
}

Tim May 19, 2010 at 9:34 am

The major difference is that with optional parameters, you have to keep them all in the same order. Overloading allows you to switch the order of the parameters around. If you want to leave optional parameters out when calling a function/method, you have to left them out from right to left.

logic_earth May 19, 2010 at 5:09 pm

Not when you have Named Parameters which C# 4.0 also introduces.

key_b May 19, 2010 at 2:19 am

I just use Pytho ;)

Anonymous May 19, 2010 at 1:27 am

Hi Craig,
In your last example “earlier versions of C#”, no need to duplicate all your code. Simply call the must complete function.
// no parameters
void DoSomething()
{
int num = 1;
string str = “optional”;
DoSomething(num, str);
}
// one parameter
void DoSomething(int num)
{
string str = “optional”;
DoSomething(num, str);
}

SlitheryImp May 19, 2010 at 12:13 am

Actually, you do not have to keep repeating the // code block when using overloads, just call the ‘full’ overload with the optional parameters:
// no parameters
void DoSomething()
{
DoSomething(1, “optional”);
}
//
// one parameter
void DoSomething(int num)
{
DoSomething(num, “optional”);
}
//
// two parameters
void DoSomething(int num, string str)
{
// code
}

Comments on this entry are closed.