If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. Thus, the default constructor is always of the form
protected C(): base() {}
or
public C(): base() {}
where C is the name of the class. If overload resolution is unable to determine a unique best candidate for the base class constructor initializer then a compile-time error occurs.
In the example
class Message
{
object sender;
string text;
}
a default constructor is provided because the class contains no instance constructor declarations. Thus, the example is precisely equivalent to
class Message
{
object sender;
string text;
public Message(): base() {}
}
Do'stlaringiz bilan baham: |