1

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 :/

koopajah
  • 23,792
  • 9
  • 78
  • 104
abc667
  • 514
  • 4
  • 19

2 Answers2

1
foo(1,2,d:true); //will call the second method.
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
0

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123