Neeraj Kumar Gupta
Neeraj Kumar Gupta

Reputation: 2363

Why default constructor does not work when parameterized constructor added?

If we have only a parameterized constructor in class then why we can not create the object with default constructor? as before adding the parameterized constructor, there was no default constructor present in class! still, an instance of lass can be created by default constructor. But after adding parameterized constructor the default constructor stop working. WHY?

can anyone explain?

class Program
{
    static void Main(string[] args)
    {
        Test test = new Test(); //instance created using parameterized constructor

        Test2 test = new Test2(); //instance can not be created using default constructor

    }

    class Test
    {
        //no constructor present
    }

    class Test2
    {
        public Test2(int a)
        {
            //parameterized constructor present
        }

    }

}

Upvotes: 1

Views: 2915

Answers (3)

Willy David Jr
Willy David Jr

Reputation: 9131

By default if no constructor was created, the default constructor is given to class to enable it to be initialized.

Now if you created a parameterized constructor, no default constructor will be created on that class, unless you defined or add that parameterless constructor.

Based on Microsoft docs this is what's happening:

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

Upvotes: -1

Selman Genç
Selman Genç

Reputation: 101681

If you don't add any constructor, the compiler adds a parameterless constructor for you as the default constructor.

If you add any constructor, the default constructor is not added. If you still want a parameterless constructor you need to add it manually.

The official docs says:

A constructor like this one, which takes no arguments, is called a default constructor.

[....]

If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

When you add a parameterized constructor you are basically saying this class needs these particular parameters in order to be properly initialized. If parameterless constructor still worked you couldn't enforce the use of that particular constructor. You would have to define a private default constructor to avoid it being used.

Upvotes: 8

Enigmativity
Enigmativity

Reputation: 117064

That's the way the language has been designed. It is there to enforce you to always specify the parameters that the author of the class has specified.

Imagine if you had a class that you had to provide a non-negative number to make it valid. If the class accepted a default constructor then you've created a situation where the class is invalid. Thus if you want the default constructor to be available then the author of the class must explicitly put it in if they've added a constructor with parameters.

You'd need to do this:

class Test2
{
    public Test2() { }

    public Test2(int a)
    {
        //parameterized constructor present
    }

}

Upvotes: 3

Related Questions