Skip to content
AgileLogics
3 min read

Method Overloading & Optional Parameters

Prerequisites: Methods

Like Java, several methods can share a name with different parameter lists (overloading). C# also supports optional parameters with a default value directly in the signature:

static double Power(double baseNum, int exponent = 2) {
    return Math.Pow(baseNum, exponent);
}

Console.WriteLine(Power(5));    // 25 — exponent defaults to 2
Console.WriteLine(Power(5, 3)); // 125

Named arguments can also be passed out of order by name:

Power(exponent: 3, baseNum: 5); // same call, named explicitly