3

Maybe my question will be weird. I have this snippet :

public class Class1
    {
        public static void Main()
        {
            Console.WriteLine(Getinfo());
            Console.ReadKey();
        }

        public static string Getinfo(string s="")
        {
            return "with param";
        }

        public static string Getinfo()
        {
            return "without param";
        }
    }

when I run this code, I have "without param" as a result . So I need to know :

  1. Why I didn't get a compiler exception ( the two methods can be executed) ?
  2. How overloading methods works if I have, as in the snippet above, methods with optionnal parameters?

Thanks,

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 1
    As long as your parameterless method with the same name exists, the default value for your first method will never be used, as the conditions to trigger it are matched by the paramaterless method.I'd need to read up on how it chooses which one to call, but I'm assuming a method that matches completely will take precedence over optional parameters – David Watts Mar 11 '15 at 12:58

0 Answers0