[C# Solution] Base Class Does Not Contain a Constructor That Takes 0 Arguments

[C# Solution] Base Class Does Not Contain a Constructor That Takes 0 Arguments
Rating: 5 - 1 Votes

[C# Solution] Base Class Does Not Contain a Constructor That Takes 0 Arguments

C#Recently, I encountered an error while creating a constructor in a derived class. The error and code is given below.

Error:

Error 1 ‘Hello_World.Customer’ does not contain a constructor that takes 0 arguments

I tried my best to analyze the issue, but somehow I was not able to figure out that why derived class is not allowing me to create a constructor and showing this annoying error.  After doing a little search over internet, I finally found the solution.

The problem was that I did not know the fact that whenever you try to call a constructor in derived class, then by default a base class parameterless constructor is called first.  Also, if you are creating a custom constructor in base class, then your default base class parameterless constructor will be overridden.  Now, in this case, there are 2 solutions.  First solution, you need to explicitly create a base class parameterless constructor.  Second solution, while creating your derived class constructor, you need to call a base class custom constructor using base keyword and passing required parameters to it.  Examples for both solutions are given below.

First Solution :  Parameterless Constructor

Second Solution :  Calling Base Class Constructor