"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Support for optional parameters in C# 4.0

Since the advent of C#, one of the popular debate happened in the IT developer community was over the support for “optional parameters”. I saw lot of threads that speaks about this and most of the developers were in favor of optional parameters. The call is over. C# 4.0 introduces the support for optional parameters, an expected feature by developers for a long time.

Need of optional parameters?

This first question arises while thinking about optional parameters is – Is it really needed? I think yes it is needed. If available, it is a good feature that gives you much flexibility in writing the code and you can reduce the overloads in the code, but of course, there are alternatives in case of unavailability of optional parameters.

Optional Parameters in C# 4.0

With C# 4.0, the optional parameters are part of the language. Defining optional parameters is simple. Consider the following example.

public String OptionalParameterTest(int i = 0, int j = 0)

{

    return String.Format("<br>The value for i : {0}, The value for j: {1}", i,j);

}

The method just returns the values of the provided parameters as a string. See the following code constructs that calls this function.

protected void Page_Load(object sender, EventArgs e)

{

    Response.Write(OptionalParameterTest());

    Response.Write(OptionalParameterTest(1));

    Response.Write(OptionalParameterTest(1,2));

}

This will print the following output.

The value for i : 0, The value for j: 0
The value for i : 1, The value for j: 0
The value for i : 1, The value for j: 2

You specify that a parameter is optional when you define a method by providing a default value for the parameter. You indicate a default value by using the assignment operator. You can invoke the method that takes optional parameters as any other method. During the method call, you can omit certain arguments and the method will use the default values you provided in the function signature.

Named Arguments

By default, C# uses the position of each argument in a method call to determine which parameters they apply to. Hence the previous example, consider the second function call.

OptionalParameterTest(1)

The value passed will be assigned to the variable “I” as it assigns the first parameter passed to the variable i. In some cases you may need to call the method by passing only the value for j. C# allows you to pass a parameter value by specifying its name. This feature allows you to pass the parameters in a different sequence.

To pass the argument as a named parameter, you need to specify the following syntax

<Parametername> :< value>

Now for the above example, if you want to call the function with the value of parameter j, you could use

OptionalParameterTest(j:1);

Add the following line to the code.

Response.Write(OptionalParameterTest(j:1));

Response.Write(OptionalParameterTest(j:1, i:2));

The output is as follows.

The value for i : 0, The value for j : 1
The value for i : 2, The value for j : 1

Named arguments give you the ability to pass arguments in any order. This can be applied to any method regardless its parameters are optional or not. Even though optional parameter and named arguments will give you some flexibility in writing your code, there may some situations where your code becomes ambiguous. When overloading methods with optional parameters, you need to write your code carefully otherwise, you will get compile time errors.

Any how this is one of the most awaited features for C# and is available for you to use.

3 Comments

Comments have been disabled for this content.