Introduced in C# 4.0, Named Arguments allow you to explicitly specify the name of an argument when calling a method.
Normally, method arguments must be passed in the order they are defined. However, by using named arguments, you can freely change the order of arguments or pass values only to specific optional arguments. This significantly improves the readability of methods that have many parameters.
In this article, I will explain how to effectively use named arguments using a product search method as an example.
Table of Contents
- Implementation Example: Usage in a Product Search Method
- Explanation and Benefits
- Summary
Implementation Example: Usage in a Product Search Method
The following code defines a method that accepts a search keyword (required), a minimum price (optional), and a maximum price (optional). It demonstrates how to call this method flexibly using named arguments.
using System;
namespace NamedArgumentsExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("--- Product Search System ---");
// 1. Normal Call (Positional Arguments)
// Values must be passed in the order of definition.
// Equivalent to SearchProducts("Keyboard", 0, 5000);
SearchProducts("Keyboard");
// 2. Specify only specific arguments (Mixed Use)
// Pass the required argument as is, skip the intermediate 'minPrice',
// and specify only 'maxPrice'.
SearchProducts("Laptop", maxPrice: 150000);
// 3. Change the order of arguments
// By using named arguments for everything, you can write them in any order.
// It becomes clear in the code "what is being passed," improving readability.
SearchProducts(maxPrice: 3000, keyword: "Mouse", minPrice: 1000);
}
/// <summary>
/// Searches and displays products matching the conditions.
/// </summary>
/// <param name="keyword">Search Keyword (Required)</param>
/// <param name="minPrice">Minimum Price (Default is 0)</param>
/// <param name="maxPrice">Maximum Price (Default is Unlimited)</param>
static void SearchProducts(string keyword, int minPrice = 0, int maxPrice = int.MaxValue)
{
string maxPriceStr = (maxPrice == int.MaxValue) ? "Unlimited" : $"{maxPrice} yen";
Console.WriteLine($"Search Execution -> Keyword: \"{keyword}\", " +
$"Price Range: {minPrice} yen - {maxPriceStr}");
}
}
}
Explanation and Benefits
1. Specifying Only Specific Optional Arguments
As shown in Example 2 above, if you want to use the default value for minPrice but change only maxPrice, without named arguments, you would have to write all arguments like SearchProducts("Laptop", 0, 150000). Using named arguments allows you to pinpoint and specify only the arguments you want to change.
2. Self-Documenting Code (Improved Readability)
By writing the argument names on the calling side, as in Example 3, it becomes immediately obvious what the number 3000 represents. Code written as Calculate(enableLogging: true, retryCount: 10) conveys the intent much more clearly than code like Calculate(true, 10).
3. Reordering Arguments
If argument names are explicit, the compiler automatically maps them, so you don’t need to worry about the order. This also has the advantage that the calling code is less likely to break even if the order of arguments in the method definition changes during refactoring.
Summary
Named arguments are extremely powerful, especially when using methods with many parameters or multiple optional arguments (arguments with default values).
- Syntax:
ArgumentName: Value - Benefits: Improved readability, ability to omit arguments, and flexibility in order.
Please try to use them actively in library method calls or complex constructors to write code that is easy to read and less prone to errors.
