Free Download Video
General

How to use named and optional parameters in C#

Microsoft introduced support for named and optional parameters in C# 4.0. While a named parameter is used to specify an argument based on the name of the argument and not the position, an optional parameter can be used to omit one or more parameters in the method signature. The parameters of a method can be either required or optional depending on whether or not you need to pass a value to these parameters when the method is called.

It should be noted that named and optional parameters can be used not only with methods but also with indexers and delegates. This article discusses these two powerful features of the C# programming language and how we can work with them.

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.

Create a console application project in Visual Studio

First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. Click Create.

This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project to work with named and optional parameters in the subsequent sections of this article.

Use named parameters in C#

When you call a method, constructor, indexer, or delegate, you must pass arguments for the required parameters but you are free to omit arguments for the parameters that have been defined as optional parameters.

You might often need to call a method that has many parameters. And even when you’re calling such a method with only the required parameters, it is sometimes extremely difficult to understand which argument maps to which parameter. Here’s where named arguments come to the rescue.

Named arguments in the C# programming language are used to associate the argument name of a method with its value — i.e., the value passed as an argument when calling the method. It should be noted that when using a named argument, the arguments are evaluated in the same order in which they were passed.

Let’s look at an example. Write the following method named Add inside the Program class of your new console application project.

public static int Add(int x, int y, int z, int a, int b, int c)
{
   return x + y + z + a + b + c;
}

Let’s suppose you’re calling the Add method as shown in the code snippet below.

static void Main(string[] args)
{
    Add(5, 10);
}

The above code won’t work because there are six required parameters (none are optional parameters) in the signature of the Add method but you have passed only two arguments. You’ll be presented with the following error.

no argument given error IDG

Figure 1. If you don’t pass arguments for all of your method’s required parameters, you will produce an error. 

Hence, you’re constrained to passing values to each of the parameters to satisfy the call as shown in the code snippet given below.

static void Main(string[] args)
{
   Add(5, 10, 8, 2, 3, 6);
}

Things get complicated when you have a mix of data types in the parameters of a method. To illustrate this, let’s modify our Add method as shown in the following code snippet.

public static int Add(int x, int y, int z, double a, double b, double c)
{
    return x + y + z + Convert.ToInt32(a) + Convert.ToInt32(b) + Convert.ToInt32(c);

}

Remembering the data types of the parameters as well as their position is cumbersome. The solution to this is to take advantage of named arguments and pass values to the method as shown in the code snippet given below.

static void Main(string[] args)
{
    Add(x:5, y:10, z:8, a:2.0, b:3.0, c:6.0);
}

You can even change the order of the named arguments, as shown in the code snippet given below.

static void Main(string[] args)
{
    Add(z: 8, x:5, y:10, c: 6, a:2.0, b:3.0);
}

As long as you name the arguments, you can pass them in any order and the compiler will not flag any error — i.e., this is perfectly valid in C#.

Use optional parameters in C#

Optional parameters in the C# programming language allow you to specify arguments in a method signature that the caller of the method is free to omit. In other words, while you must specify values for required parameters, you might choose not to specify values for optional parameters. In some cases, an optional parameter might have a default value associated with it as well.

The default value of an optional parameter may have any of three kinds of values: a constant expression, an expression that is of the form of a value type, or an expression that is of the form of default(v) where v is a value type.

The Add method shown in the following code snippet illustrates how you can specify optional arguments to a method in C#.

public static int Add(int x, int y=0, int z=0)
{
    return x + y + z;
}

And here is how you can call the Add method.

Add(10);

Because two of the parameters in the Add method are optional, you can pass a single integer value to the method when calling it. Take care to follow the proper order of defining parameters in a method. The required parameters should come first, followed by optional parameters if any exist.

Named and optional parameters were introduced to the C# programming language in order to improve interoperability with COM APIs. Using named parameters can improve the readability of the source code. And you can take advantage of optional parameters as a replacement for using overloaded methods when the method definition is identical.

How to do more in C#:

Related posts

How to Survive a Crisis with AI-Driven Operations

admin

A prison video visitation service exposed private calls between inmates and their attorneys

admin

7 low-code platforms developers should know

admin