Optional Parameters and Named Arguments in C# 4.0


Optional Parameters and Named Arguments

When a function has an optional parameter, the caller can decide whether to provide a value or not. (Providing a value for the parameter is optional.) If the caller doesn't provide a value, the function uses a default value.

C++ allows optional parameters. In this example, b is an optional parameter. If the caller doesn't provide a value for b, the function uses the default value, zero.

int DoSomething(int a, int b = 0)

{
        return a + b; 
}

This can be called as follows:

// Returns 5 + 7 = 12;
int x = DoSomething(5, 7);
 
// Returns 5 + 0 = 5
int y = DoSomething(5);

VB.Net has allowed optional parameters since version 1.0. C#, however, did not support optional parameters until .NET 4.0.

Method overloading

Prior to .NET 4.0, the closest you could come to optional parameters was method overloading. Here, for example, the method DoSomething has an optional parameter with a default value of 0.

public int DoSomething(int a, int b)
{
        return a + b;
}
 
public int DoSomething(int a)
{
        Return DoSomething(a, 0);

}

Method overloading has its problems. For one thing, every optional parameter can double the number of overloads. DoSomething( ) has one optional parameter and two overloads. If we have two optional parameters, there are four overloads:

public void DoSomethingElse(string s, decimal d)
{
        Console.WriteLine( "{0}, {1}" , s, d);
}
 
public void DoSomethingElse(string s)
{
        DoSomething(s, 5);
}
 
public void DoSomethingElse(decimal d)
{
        DoSomething("Hello", d);
}
 
public void DoSomethingElse( )
{
        DoSomething("Hello", 5);
}

This can get out of hand quickly. It's also error prone.

Optional Parameters

Optional parameters make life easier. Here is one function to replace the four overloaded methods shown above:

public void DoSomethingElse(string s = "Hello", decimal d = 5)
{
        Console.WriteLine("{0}, {1}", s, d);
}

There are a few restrictions.
  1. All optional parameters must come after the required parameters. So this is invalid:

    // Invalid:  optional parameter before required parameter
    public void DoSomething(string s = "Hello", decimal d)
    {
          ...
    }
  2. The default value for an optional parameter must be a constant. It can be a string or a number, const, null, or an enum.
     
    // Invalid:  optional parameter is not a constant.
    public void DoSomething(DateTime dt = DateTime.Now)
    {
          ...
    }

    One possible way to overcome this restriction is as follows:
     
    // Setting a default value of DateTime.Now
    public void DoSomething(Nullable<DateTime> dt = null)
    {
            if (!dt.HasValue)
                   dt = DateTime.Now;
    }

Optional Parameters With Named Arguments

One case where optional parameters run into trouble is when there is more than one optional parameter with the same data type. For example:

public void DoSomething(int a = 5, int b = 10)
{
        ...
}

In this case, it's not clear what a call to DoSomething(37) means. Are you passing in a or b? This is where named arguments come in handy. Named parameters (also new to .NET 4.0) allow you to specify which parameter you intended.

// a = 37.  b = the default value.
DoSomething(a: 37);
 
// a = the default value.  b = 37.
DoSomething(y: 37);

Beware

Using optional parameters in class libraries can cause unexpected problems. Suppose I have a class library that contains the following method:

public int DoSomething( int x = 30)
{
        return x;
}

I wrote a program that uses the class library and calls DoSomething( ). DoSomething returns the default value, 30.

Now suppose I modify the class library and change the default value:

public int DoSomething( int x = 40 )
{
        return x;
}

I recompile the class library and then run my program again. I call DoSomething( ) and it still returns 30.

The only way I can get the correct result is if I recompile my program as well.
 

Up Next
    Ebook Download
    View all
    Learn
    View all