Named and Optional Parameters in C#

Named and Optional Parameters in C#

In this blog post, we will explain the concept of named and optional arguments in C# using pizza as an example.

What are named and optional arguments?

Named and optional arguments are features of C# that allow you to specify the parameters of a method call in a flexible and convenient way. Named arguments let you specify the value of a parameter by its name rather than by its position in the parameter list. Optional arguments let you omit the value of a parameter if it has a default value defined in the method declaration. Named and optional arguments can help you improve the readability and maintainability of your code by making it clear what each argument means and reducing the number of arguments you need to pass.
For example, suppose you have a method that creates a pizza object based on some parameters, such as size, crust, cheese, sauce, and toppings. The method declaration looks like this:

public Pizza CreatePizza(string size, string crust = "Thin", bool cheese = true, string sauce = "Tomato", params string[] toppings)
{
    // create and return a pizza object based on the parameters
}

The method has one required parameter (size) and four optional parameters (crust, cheese, sauce, and toppings). The optional parameters have default values that will be used if no value is provided for them. The toppings parameter is also a params parameter, which means it can accept zero or more values as an array.
Now, let's see how we can use named and optional arguments to call this method in different ways.

Using positional arguments:

The simplest way to call this method is to use positional arguments, which means passing the values of the parameters in the same order as they are declared in the method. For example:

Pizza pizza1 = CreatePizza("Large"); // creates a large pizza with thin crust, cheese, tomato sauce, and no toppings
Pizza pizza2 = CreatePizza("Medium", "Thick", false); // creates a medium pizza with thick crust, no cheese, tomato sauce, and no toppings
Pizza pizza3 = CreatePizza("Small", "Stuffed", true, "Barbecue", "Chicken", "Mushrooms"); // creates a small pizza with stuffed crust, cheese, barbecue sauce, and chicken and mushrooms as toppings

Using positional arguments is straightforward and concise, but it has some drawbacks. For example:

  • You need to remember the order of the parameters and their default values.

  • You need to provide values for all the parameters before the one you want to change. For example, if you want to change the sauce of a large pizza to barbecue, you need to provide values for size, crust, and cheese as well.

  • You need to use empty or dummy values for the parameters you want to skip. For example, if you want to create a large pizza with no cheese and no toppings, you need to pass false for cheese and an empty array for toppings.

Using named arguments:

A better way to call this method is to use named arguments, which means specifying the name of each parameter along with its value. For example:

// creates a large pizza with thin crust, cheese, barbecue sauce, and no toppings
Pizza pizza4 = CreatePizza(size: "Large", sauce: "Barbecue"); 
// creates a medium pizza with thin crust, no cheese, tomato sauce, and pepperoni and olives as toppings
Pizza pizza5 = CreatePizza(size: "Medium", cheese: false, toppings: new string[] {"Pepperoni", "Olives"}); 
// creates a small pizza with stuffed crust, cheese, pesto sauce, and spinach and feta as toppings
Pizza pizza6 = CreatePizza(size: "Small", crust: "Stuffed", cheese: true, sauce: "Pesto", toppings: new string[] {"Spinach", "Feta"});

Using named arguments has some advantages over positional arguments. For example:

  • You don't need to remember the order of the parameters or their default values. You can specify them in any order you like.

  • You don't need to provide values for all the parameters. You can only provide values for the ones you want to change or customize.

  • You don't need to use empty or dummy values for the parameters you want to skip. You can simply omit them from the method call.

Using optional arguments:

Another way to call this method is to use optional arguments, which means omitting the values of the parameters that have default values defined in the method declaration. For example:

// same as pizza1
Pizza pizza7 = CreatePizza("Large"); 
// same as pizza2
Pizza pizza8 = CreatePizza("Medium", "Thick", false); 
// creates a small pizza with thin crust, cheese, tomato sauce, and ham and pineapple as toppings
Pizza pizza9 = CreatePizza("Small", toppings: new string[] {"Ham", "Pineapple"});

Using optional arguments is similar to using positional arguments, but it allows you to skip the parameters that have default values. This can make your code more concise and clear, but it also has some drawbacks. For example:

  • You still need to remember the order of the parameters and their default values.

  • You still need to provide values for all the parameters before the one you want to change or customize.

  • You still need to use empty or dummy values for the params parameter if you want to skip it.

Using a combination of named and optional arguments:

The best way to call this method is to use a combination of named and optional arguments, which means specifying the name of some parameters and omitting the values of others. For example:

// same as pizza4
Pizza pizza10 = CreatePizza(size: "Large", sauce: "Barbecue"); 
// same as pizza5
Pizza pizza11 = CreatePizza(size: "Medium", cheese: false, toppings: new string[] {"Pepperoni", "Olives"}); 
// same as pizza6
Pizza pizza12 = CreatePizza(size: "Small", crust: "Stuffed", sauce: "Pesto", toppings: new string[] {"Spinach", "Feta"});

Using a combination of named and optional arguments gives you the best of both worlds. You can specify the parameters you want to change or customize by their name and omit the ones that have default values. This makes your code more readable and maintainable and avoids the drawbacks of using positional or optional arguments alone.

Hope this helped you get to know about named and optional arguments in C#. Let's meet in the next blog.

Did you find this article valuable?

Support Vignesh Ponnuvel by becoming a sponsor. Any amount is appreciated!