Answer: We can’t declare a default constructor (a constructor with no parameters) for a structure. The following example would compile if Time were a class, but because Time is a structure, it does not:
struct Time
{
public Time() { ... } // compile-time error
...
}
The reason we can’t declare our own default constructor for a structure is that the compiler always generates one. In a class, the compiler generates the default constructor only if we don’t write a constructor ourself. The compiler-generated default constructor for a structure always sets the fields to 0, false, or null—just as for a class.
Asked In: Many Interviews |
Alert Moderator