Named Parameter


In this article we are going to discuss about named parameter with examples in C#.

What are named parameters in a method?

Named parameters allow you to specify arguments by the parameter name rather than by position. This improves code readability and avoids errors, especially with methods that have many parameters.

public void DisplayMessage(string message, int repeatCount = 1, bool toUpper = false)
{
    if (toUpper)
    {
        message = message.ToUpper();
    }
    for (int i = 0; i < repeatCount; i++)
    {
        Console.WriteLine(message);
    }
}

static void Main()
{
    DisplayMessage(message: "Hello", repeatCount: 2, toUpper: true); // Output: HELLO HELLO
}

Prev Next