0
  class Program
    {
        static void Main(string[] args)
        {
            Program a = new Program();
            Console.ReadLine();

        }

        public Program()
        {
            Console.WriteLine("constructor without parameters");
        }

        public Program(int a = 0)
        {
            Console.WriteLine("optional parameters constructor");
        }
    }

Why is the second constructor used and why is it not forbidden (same signature)?

Fabian
  • 167
  • 1
  • 9
  • 2
    Please read [ask] and create a [mcve]. This program does _not_ call the parameterized constructor, and the signatures are not the same anyway. – CodeCaster Apr 10 '17 at 11:27
  • thanks i did search, but did not find the related answers – Fabian Apr 10 '17 at 11:29
  • The second should only be called if you actually pass an `int`, thus making the optional parameter useless. – juharr Apr 10 '17 at 11:31
  • 1
    @Fabian -- I always wonder about a response like yours: "I did search but didn't find anything." This is 2017; search engines have been around since I believe 1996, 21 years. I plugged in a very simple search phrase in google and came up with the duplicate answer within the first 2 or 3 results. So I'm not buying that you did a search. – rory.ap Apr 10 '17 at 11:34
  • i found answers on what constructor is choosen, but i didn't find any with a reason. – Fabian Apr 10 '17 at 12:01
  • @Fabian -- Maybe if you took a step back and realized this behavior is not just limited to constructors, but rather applies to all methods. – rory.ap Apr 10 '17 at 12:08

0 Answers0