;

Named Arguments in C#


Tutorialsrack 07/08/2022 C#

In this article, you’ll learn about what is named arguments in C#. And how you can use named argument in C#.

Named Arguments

In C# 4.0, named arguments were introduced. Named arguments to enable you to specify an argument for a parameter by matching the argument with its name rather than with its order in the parameter list. 

When you use named arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list. It makes your code more readable and clean.

The only restriction is that named arguments must be placed after the unnamed arguments. Parameter names can be specified for both optional and required parameters and can be used to improve readability or arbitrarily to reorder arguments in a call.

For instance, you use the syntax "sellerName:" and productName:and then a string literal to specify the value of the name parameter. The syntax "orderNum:" and then an integer signifies the size parameter.

Learn about: Optional Parameter in C#

Here is an example of Named Arguments in C#

Example: Named Argument in C#

Example: Named Argument in C#
using System;
 
namespace Program
{
    public class Program
    {
        //Named Parameter in c#
 
        static void PrintOrderDetails(string sellerName, int orderNum, string productName)
        {
            if (string.IsNullOrWhiteSpace(sellerName))
            {
                throw new ArgumentException(message: "Seller name cannot be null or empty.", paramName: nameof(sellerName));
            }
 
            Console.WriteLine($"Seller: {sellerName}, Order #: {orderNum}, Product: {productName}");
        }
        public static void Main()
        {
            // The method can be called in the normal way, by using positional arguments.
            PrintOrderDetails("TutorialsRack", 111, "C# Book");
 
            // Named arguments can be supplied for the parameters in any order.
            PrintOrderDetails(orderNum: 111, productName: "C# Book", sellerName: "TutorialsRack");
            PrintOrderDetails(productName: "C# Book", sellerName: "TutorialsRack", orderNum: 111);
 
            // Named arguments mixed with positional arguments are valid
            // as long as they are used in their correct position.
            PrintOrderDetails("TutorialsRack", 111, productName: "C# Book");
            PrintOrderDetails(sellerName: "TutorialsRack", 111, productName: "C# Book");    // C# 7.2 onwards
            PrintOrderDetails("TutorialsRack", orderNum: 111, "C# Book");                   // C# 7.2 onwards
 
            // However, mixed arguments are invalid if used out-of-order.
            // The following statements will cause a compiler error.
            // PrintOrderDetails(productName: "Red Mug", 31, "Gift Shop");
            // PrintOrderDetails(31, sellerName: "Gift Shop", "Red Mug");
            // PrintOrderDetails(31, "Red Mug", sellerName: "Gift Shop");
 
            Console.ReadLine();
        }
    }
}
Output

Seller: TutorialsRack, Order #: 111, Product: C# Book

Seller: TutorialsRack, Order #: 111, Product: C# Book

Seller: TutorialsRack, Order #: 111, Product: C# Book

Seller: TutorialsRack, Order #: 111, Product: C# Book

Seller: TutorialsRack, Order #: 111, Product: C# Book

Seller: TutorialsRack, Order #: 111, Product: C# Book

Remove This Line it is needed for escape from div


Related Posts



Comments

Recent Posts
Tags