abc667
abc667

Reputation: 514

Call overload method with default parameters

Suppose we have two methods

public void foo(int a, int b, bool c = false)
{
    //some code
}

public void foo(int a, int b, bool d, bool c = false)
{
    //some other code
}

when I call foo(1,2,true) it refers to first method. Is there any way to call second method by passing only 3 parameters?

I found something like this in production code :/

Upvotes: 1

Views: 143

Answers (2)

Tigran
Tigran

Reputation: 62296

Named parameter is just a parameter, with default value.

In your presented code you simply overload. Note the amount of parameters (non named) is different.

Other question could be:

Can I do something like this ?

public void foo(int a, int b, bool c)
{
    //some code
}

public void foo(int a, int b, bool c = false)
{
    //some other code
}

Answer: no, you can not. Because, as I said, named parameter, is just an ordinary parameter with default value, so this will not compile, as there is already one method with exactly same signature.

Upvotes: 0

It'sNotALie.
It'sNotALie.

Reputation: 22814

foo(1,2,d:true); //will call the second method.

Upvotes: 1

Related Questions