1

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
        }

    }

}
2
  • Well, if you have such question, then what your class inherits from? :) Commented Aug 26, 2018 at 13:03
  • Related softwareengineering.stackexchange.com/a/257948 Commented Aug 26, 2018 at 13:12

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The OP asks "Why?" You haven't explained why.
Ofcourse I did, " the default constructor stop working. WHY?" > it stops working because there is no default constructor added when there is another constructor. What else do you expect?
I have added further clarification, hope it's better.
You had explained the what, not the why. Your final paragraph does the job nicely.
The ultimate answer of why is as you said in you answer "That's the way the language has been designed." we can only speculate as to why it is designed this way. But the OPs question was "Why I can't use the default constructor", it wasn't why the language designed this way.
|
3

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
    }

}

1 Comment

Thanks @Enigmativity, I have explained the same situation to interviewer, same as yours, but he was expecting background process to handle the full scenario, I Think he was expecting this line of answer "The compiler adds a parameterless constructor for you as the default constructor." ..
-1

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.

1 Comment

The OP asks "Why?" You haven't explained why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.