1

Say I have these 2 functions:

    public void Test(int Param1)
    {
        //do something
    }

    public void Test(int Param1, int Param2 = 1)
    {
        //do something
    } 

If I execute this: Test(2)

How does the compiler know which function to execute? It could be any of them: the first one which only accepts 1 parameter or the second one which accepts 1 or 2 parameters.

In fact I'm surprised that the above code even compiles.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
Anthony
  • 7,210
  • 13
  • 60
  • 70
  • I encourage you to read section 7.5.3.2 "Better Function Member" of the C# 4 specification. – Eric Lippert May 08 '13 at 20:34
  • http://msdn.microsoft.com/en-us/library/dd264739.aspx "If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters." – K0D4 May 08 '13 at 21:27

0 Answers0