How to Use Optional Parameters in C# 4.0

Contributing Editor

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!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.google.ie SlitheryImp

    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
    }

  • Anonymous

    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);
    }

  • key_b

    I just use Pytho ;)

  • Tim

    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.

    • http://logicearth.wordpress.com logic_earth

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

  • anon

    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
    }

  • http://www.optimalworks.net/ Craig Buckler

    @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

      good point, thanks for the observation!