Can we declare a default constructor (a constructor with no parameters) for a structure?

 Posted by Kundan64 on 1/29/2013 | Category: C# Interview questions | Views: 4062 | Points: 40
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 

Comments or Responses

Login to post response