Information Technology Reference
In-Depth Information
Default Constructors
If no instance constructor is explicitly supplied in the class declaration, then the compiler sup-
plies an implicit, default constructor with
No parameters
An empty body
If the programmer defines any constructor at all, then the compiler does not define any
default constructors for the class.
For example,
Class2
declares two constructors.
Because there is at least one explicitly defined constructor, the compiler does not create
any additional constructors.
In
Main
, there is an attempt to create a new instance using a constructor with no param-
eters. Since there
is
no constructor with zero parameters, the compiler will produce an
error message.
class Class2
{
public Class2(int Value) { ... } // Constructor 0
public Class2(String Value) { ... } // Constructor 1
}
class Program
{
static void Main()
{
Class2 a = new Class2(); // Error! No constructor with 0 parameters
...
}
}