0

Consider two methods with same name, but different parameters:

private void Method(int number) => Debug.WriteLine("I'm method 01");

private void Method(int number, string name = "") => Debug.WriteLine("I'm method 02");

Theoretically if we call Method(0);, both methods are applicable. There are no errors/warnings.

Debugging shows that the compiler takes the method 01 - is there any rule for that?

Is there no danger that method 02 gets invoked?

Romasz
  • 29,662
  • 13
  • 79
  • 154

1 Answers1

2

The C# specification explains at length exactly how overload resolution works.

See here for precise details.

C# is not a random language; every possible piece of code either has a spec-defined meaning (which will never change) or will give a compiler error.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964