Working With Optional Arguments in C#

Optional arguments were introduced in C# 4. They are valid for constructors, indexers, methods, and delegates. In this post, I will cover the basic information needed to use them correctly.

What are Optional Arguments?

Optional arguments are exactly what they sound like: arguments that you are not required to supply because they have a default value already specified. If no value is sent for that argument, the default value is used. If you don’t want the default value to be used you simply supply another value.

Rules for Optional Arguments

Optional arguments do have to follow a couple of rules, though. First, in the argument list of the method, constructor, etc they must be defined after all required arguments.

Listing 1.1 – Optional Argument Example

public void DoSomething(int reqArg1, int reqArg2, int reqArg3, int optArg1 = 1, int optArg2 = 10, string optArg3 = "Hello")
{
    Console.WriteLine($"reqArg1: {reqArg1}, reqArg2: {reqArg2}, reqArg3: {reqArg3}, optArg1: {optArg1}, optArg2: {optArg2}, optArg3: {optArg3}");

    Console.ReadKey();
}

In this example, the three required arguments are defined first and the three optional arguments are defined last. You can tell that they are optional because they have a value assigned to them in the method signature.

Second, the types of values that can be assigned to an optional argument are limited to one of three scenarios:

  1. A constant value such as 1, 10, or “Hello”.
  2. An expression in the form of a new(valType) declaration where valType is a value type.
  3. An expression in the form of a default(valType) declaration where valType is a value type.

Third, you have two options for using the optional arguments. You can either supply a value for each of the optional arguments that come before the one that you want to give a different argument for.

Listing 1.2 – Using all optional arguments before the argument you want to supply a different value for.

DoSomething(10, 20, 30, 1, 50);

What we are really after in the example above is setting the value of optArg2 to 50. To do so, we have to set optArg1 as well.

There is another way, however. We can use named arguments. Instead of passing 1 to optArg1 and 50 to optArg2, we can just specify optArg2 using named arguments.

DoSomething(10, 20, 30, optArg2: 50);

One issue can arise from using named arguments, however. If you have two different methods that have two different signatures but have the same named arguments that are being used, the compiler will not know which you are trying to use and will result in a compile-time error. The only way to fix this is to change the name of the named argument being used. This is not likely to be a common problem, however, because the use of optional arguments should limit the need for multiple method overloads.

Conclusion

In this post, we explored optional arguments, what they are, and how to use them. They are not difficult to use and they can provide us with a much cleaner codebase when used appropriately.

Leave a Reply

Your email address will not be published.