Named and Optional Arguments in C#

Introduction

This article explains the following two things that we must be aware of as programmers:

  • Named Arguments
  • Optional Arguments

Both of this were introduced with Visual Studio 2010. Now we will proceed in detail about those. I hope you will like it.

Please see this article in my blog: Named And Optional Arguments.

Background

I am working in a project in which we are using Visual Studio 2012 and C# as the programming language, We do have many functions in our logical layers. In those functions we used both Named and Optional Arguments. So I thought of sharing this with you. Please be noted that this article is for the one who have not tried Named and Optional arguments until now.

Before going to the coding part, we will learn what Named and Optional arguments are. What are the features of these two?

Named Arguments

Reference, MSDN: Named arguments enable you to specify an argument for a specific parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.

As said by MSDN, a named argument:

  • Enables you to pass the argument to the function by associating the parameter's name.
  • No need for remembering the parameter's position that we are not aware of always.
  • No need to look at the order of the parameters in the parameters list of the called function.
  • We can specify the parameter for each argument by its name.

Now we will describe all about a named argument with a simple program. I hope we all know how to determine the area of a rectangle. Yes, you are right, it is A= wl (where w is the width and l is length and A is area). So we will be using this formula in our function. Consider the following is our function call.

  1. FindArea(120, 56);  

In this our first argument is length (120) and the second argument is width (56). And we are calculating the area by that function. And the following is the function definition.

  1. private static double FindArea(int length, int width)  
  2. {  
  3.    try  
  4.    {  
  5.       return (length* width);  
  6.    }  
  7.    catch (Exception)  
  8.    {  
  9.       throw new NotImplementedException();  
  10.    }  
  11. }  

So in the first function call, we just passed the arguments by its position.

  1. double area;  
  2. Console.WriteLine("Area with positioned argument is: ");  
  3. area = FindArea(120, 56);  
  4. Console.WriteLine(area);  
  5. Console.Read();  

If you run this, you will get an output as follows.

Now here comes the features of named arguments. Please see the preceding function call.

  1. Console.WriteLine("Area with Named argument is: ");  
  2. area = FindArea(length: 120, width: 56);  
  3. Console.WriteLine(area);  
  4. Console.Read();  

Here we are giving the named arguments in the method call.

  1. area = FindArea(length: 120, width: 56);  

Now if you run this program, you will get the same result. Please see the following image.

As I said above, we can provide the names in reverse in the method call if we are using the named arguments, right? Please see the preceding method call.

  1. Console.WriteLine("Area with Named argument vice versa is: ");  
  2. area = FindArea(width: 120, length: 56);  
  3. Console.WriteLine(area);  
  4. Console.Read();  

Please run the program and you can see the following output.

You got the same result, right? I hope you said yes.

One of the important uses of a named argument is when you use this in your program it improves the readability of your code. It simply says what your argument is meant to be, or what it is.

Now you can provide the positional arguments too. That means, a combination of both positional argument and named argument. So shall we try that?

  1. Console.WriteLine("Area with Named argument Positional Argument : ");  
  2. area = FindArea(120, width: 56);  
  3. Console.WriteLine(area);  
  4. Console.Read();  

In the preceding example we passed 120 as the length and 56 as a named argument for the parameter width.

I hope you enjoyed using named arguments, there are some limitations too. We will explain the limitation of named arguments now.

Limitation of using a Named Argument

Named argument specification must appear after all fixed arguments have been specified.

If you use a named argument before a fixed argument you will get a compile time error as follows.

Named argument specification must appear after all fixed arguments have been specified

Optional Arguments

Reference, MSDN: The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.

As said in the MSDN, an Optional Argument:

  • We can omit the argument in the call if that argument is an Optional Argument.
  • Every Optional Argument has its own default value.
  • It will take a default value if we do not supply the value.
  • A default value of an Optional Argument must be a:
    1. Constant expression.
    2. Must be a value type such as enum or struct.
    3. Must be an expression of the form default (valueType)
  • It must be set at the end of parameter list

Now consider the preceding is our function definition with optional arguments.

  1. private static double FindAreaWithOptional(int length, int width=56)  
  2. {  
  3.    try  
  4.    {  
  5.       return (length * width);  
  6.    }  
  7.    catch (Exception)  
  8.    {  
  9.       throw new NotImplementedException();  
  10.    }  
  11. }  

Here we have set the value for width as optional and gave value as 56, right? Now we will try to call this function.

If you note, the IntelliSense itself shows you the optional argument as shown in the following image.

Now if you call the function as shown in the preceding code block. The function will be fired and give you the same output.

  1. Console.WriteLine("Area with Optional Argument : ");  
  2. area = FindAreaWithOptional(120);  
  3. Console.WriteLine(area);  
  4. Console.Read();  

Note that we did not get any error when compiling and it will give you an output as follows.

Conclusion

I hope you will like this article. Please share with me your valuable thoughts and comments. Your feedback is always welcomed.

Thanks in advance. Happy coding!