Optional Parameter


In this article we are going to discuss about Optional Parameter with examples in C#.

What are Optional Parameters in a Method?

Optional parameters allow you to omit arguments for some parameters. These parameters must have default values defined in the method signature.

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

static void Main()
{
    DisplayMessage("Hello"); // Output: Hello
    DisplayMessage("Hello", 3); // Output: Hello Hello Hello
}

Prev Next